【问题标题】:How do I show the end screen when I touch the enemies in unity统一触摸敌人时如何显示结束画面
【发布时间】:2019-08-19 17:06:05
【问题描述】:

我正在统一制作一个 2D 游戏,我搜索了解决方案,但我没有找到任何东西。

这是一个统一的新游戏

using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed = 6f;            // The speed that the player will move at.

    Vector3 movement;                   // The vector to store the direction of the player's movement.
    Animator anim;                      // Reference to the animator component.
    Rigidbody playerRigidbody;          // Reference to the player's rigidbody.
    int floorMask;                      // A layer mask so that a ray can be cast just at gameobjects on the floor layer.
    float camRayLength = 100f;          // The length of the ray from the camera into the scene.

    void Awake ()
    {
        // Create a layer mask for the floor layer.
        floorMask = LayerMask.GetMask ("Floor");

        // Set up references.
        anim = GetComponent <Animator> ();
        playerRigidbody = GetComponent <Rigidbody> ();
    }

    void FixedUpdate ()
    {
        // Store the input axes.
        float h = Input.GetAxisRaw ("Horizontal");
        float v = Input.GetAxisRaw ("Vertical");

        // Move the player around the scene.
        Move (h, v);

        // Turn the player to face the mouse cursor.
        Turning ();

        // Animate the player.
        Animating (h, v);
    }

    void Move (float h, float v)
    {
        // Set the movement vector based on the axis input.
        movement.Set (h, 0f, v);

        // Normalise the movement vector and make it proportional to the speed per second.
        movement = movement.normalized * speed * Time.deltaTime;

        // Move the player to it's current position plus the movement.
        playerRigidbody.MovePosition (transform.position + movement);
    }

    void Turning ()
    {
        // Create a ray from the mouse cursor on screen in the direction of the camera.
        Ray camRay = Camera.main.ScreenPointToRay (Input.mousePosition);

        // Create a RaycastHit variable to store information about what was hit by the ray.
        RaycastHit floorHit;

        // Perform the raycast and if it hits something on the floor layer...
        if(Physics.Raycast (camRay, out floorHit, camRayLength, floorMask))
        {
            // Create a vector from the player to the point on the floor the raycast from the mouse hit.
            Vector3 playerToMouse = floorHit.point - transform.position;

            // Ensure the vector is entirely along the floor plane.
            playerToMouse.y = 0f;

            // Create a quaternion (rotation) based on looking down the vector from the player to the mouse.
            Quaternion newRotation = Quaternion.LookRotation (playerToMouse);

            // Set the player's rotation to this new rotation.
            playerRigidbody.MoveRotation (newRotation);
        }
    }

    void Animating (float h, float v)
    {
        // Create a boolean that is true if either of the input axes is non-zero.
        bool walking = h != 0f || v != 0f;

        // Tell the animator whether or not the player is walking.
        anim.SetBool ("IsWalking", walking);
    }

我在这里尝试了一些代码,但在我的游戏中并没有达到我想要的效果。没有健康。

【问题讨论】:

    标签: unity3d


    【解决方案1】:

    您可以使用函数OnCollisionEnter2D 来检查您的玩家是否与敌人发生碰撞。每当 GameObject 进入另一个 GameObject 的 hit box 时,都会触发此函数

    例如:

    void OnCollisionEnter2D(Collision2D other) {
        if (other.gameObject.tag == "enemy") {
            // Gameover... show end screen
        }
    }
    

    别忘了在编辑器中为你的敌人设置“标签”。

    欲了解更多信息,请查看以下链接

    【讨论】:

      【解决方案2】:

      我假设您是 unity 的新用户,所以我会比平时更多地帮助您编写代码。

      为了在 2d 中检测与敌人的碰撞,我们使用函数OnCollisionEnter2d,它的作用基本上是检测 2d 中的碰撞并将碰撞信息存储到变量中,here 是文档...请阅读了解更多。

      所以在您编写了OnCollisionEnter2d 函数之后,您需要在其中编写一个if 语句,该语句使用存储在变量中的OnCollisionEnter2d 信息。并检查其他游戏对象的标签是否与您在代码中编写的内容兼容。

      那么您必须创建一个函数来启用和禁用游戏的 UI 组件。因为我不知道那些组件是什么,所以我不会写那些代码,我只会写一些例子。 here 是一些可能有帮助的文档。

      代码如下:

      void OnCollisionEnter2d(Collision2d other)
      {
       if (other.gameObject.tag == "enemy")
      {
        disableEnableUI();
      }
      
      }
      

      这里是disableEnableUI(); 函数:

      public GameObject restartButton;
      public GameObject gameOverTxt;
      void disableEnableUI()
      {
       restartButton.SetActive(true);
       gameOverTxt.SetActive(true);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多