简体   繁体   English

团结:玩家在物体碰撞时死亡

[英]Unity: Player Death on object collision

I am extremely close to finishing my game, every code I posted works well but I want my player to die whether it collides with the box (which is the enemy). 我非常接近完成游戏,我发布的每个代码都可以正常工作,但是我希望我的玩家死于它是否与盒子(是敌人)相撞。 However, I've tried to do some research and I can't seem to find the solution. 但是,我尝试进行一些研究,但似乎找不到解决方案。 How do I do this? 我该怎么做呢? Here's the code for the Player (JugadorScript.cs): 这是播放器的代码(JugadorScript.cs):

using UnityEngine;
using System.Collections;

public class JugadorScript : MonoBehaviour
{

    public float velocidad = -10f;
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {

    }
    public void moverIzquierda()
    {
        transform.Translate(Vector2.right * velocidad * Time.deltaTime);
        transform.eulerAngles = new Vector2(0, 0);
    }
    public void moverDerecha()
    {
        transform.Translate(Vector2.right * velocidad * Time.deltaTime);
        transform.eulerAngles = new Vector2(0, 180);
    }
}

The EnemySpawner.cs code, which works excellent: EnemySpawner.cs代码,效果很好:

using UnityEngine;
using System.Collections;
using System;

public class EnemySpawner : MonoBehaviour
{

    public GameObject BlockPrefab;

    float maxSpawnRateInSeconds = 2.5f;

    void Start()
    {
        Invoke("SpawnEnemy", maxSpawnRateInSeconds);
        InvokeRepeating("IncreaseSpawnRate", 0f, 30f);
    }

    // Update is called once per frame
    void Update()
    {

    }
    void SpawnEnemy()
    {
        Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));

        Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1));

        GameObject anEnemy = (GameObject)Instantiate(BlockPrefab);
        anEnemy.transform.position = new Vector2(UnityEngine.Random.Range(min.x, max.x), max.y);


        ScheduleNextEnemySpawn();
    }

    void ScheduleNextEnemySpawn()
    {
        float spawnInNSeconds;

        if (maxSpawnRateInSeconds > 1f)
        {
            spawnInNSeconds = UnityEngine.Random.Range(1f, maxSpawnRateInSeconds);
        }
        else
            spawnInNSeconds = 1f;

        Invoke("SpawnEnemy", spawnInNSeconds);
    }

    void IncreaseSpawnRate()
    {
        if (maxSpawnRateInSeconds > 1f)
            maxSpawnRateInSeconds--;

        if (maxSpawnRateInSeconds == 1f)
            CancelInvoke("IncreaseSpawnRate");
    }


}

And the BlockScript.cs, which is my enemy script: 还有BlockScript.cs,这是我的敌人脚本:

using UnityEngine;
using System.Collections;

public class BlockScript : MonoBehaviour
{

    private GameObject wayPoint;
    private Vector3 wayPointPos;
    private Rigidbody2D rigidBody2D;
    public bool inGround = true;
    private float jumpForce = 400f;

    private float speed = 6.0f;
    void Start()
    {

        wayPoint = GameObject.Find("wayPoint");
    }

    private void awake()
    {
        rigidBody2D = GetComponent<Rigidbody2D>();
    }


    void Update()
    {

        if (inGround)
        {
            inGround = false;

            rigidBody2D.AddForce(new Vector2(0f, jumpForce));
        }

        wayPointPos = new Vector3(wayPoint.transform.position.x, transform.position.y,
            wayPoint.transform.position.z);

        transform.position = Vector3.MoveTowards(transform.position,
            wayPointPos, speed * Time.deltaTime);

        Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0));

        if (transform.position.y < min.y)
        {
            Destroy(gameObject);
        }



    }


}

As you can see in the doc, the function you are looking for is OnCollisionEnter. 如您在文档中所见,您正在寻找的功能是OnCollisionEnter。

Check this tutorial: 查看本教程:

https://unity3d.com/es/learn/tutorials/topics/physics/detecting-collisions-oncollisionenter https://unity3d.com/es/learn/tutorials/topics/physics/detecting-collisions-oncollisionenter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM