首页 > 解决方案 > 统一触摸某个图层时如何重置播放器的位置?

问题描述

当我触摸“死亡”层时,我正在尝试重置我的玩家的位置(到 0、2、0)。我该怎么做?

我试过使用bool被调用isDead,我的代码看起来像这样:

if(isDead)
{
   transform.position = new Vector3(0f, 2f,0f);
}

这是我的全部代码,可能会有所帮助

using System.Collections.Generic;
using UnityEngine;

public class PlayerMovementScript : MonoBehaviour
{
    public CharacterController controller;

    public float speed = 12f;

    public float gravity = -9.81f;

scripts
    public Transform groundCheck;
    public float groundDistance = 0.4f;
    public LayerMask groundMask;
    public float jumpHeight = 3f;

    Vector3 velocity;
    bool isGrounded;
    bool isDead;
    // Update is called once per frame
    void Update()
    {

        isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);


for evig når den treffer bakken
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f;
        }

            float x = Input.GetAxis("Horizontal");
        float z = Input.GetAxis("Vertical");

        Vector3 move = transform.right * x + transform.forward * z;

        controller.Move(move * speed * Time.deltaTime);
        controller.Move(velocity * Time.deltaTime);

        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        velocity.y += gravity * Time.deltaTime;
    }
}

我不知道该怎么做。有什么建议么?谢谢!

标签: c#visual-studiounity3d

解决方案


如果死亡层上的碰撞器是触发器,则需要OnTriggerEnter,否则需要OnCollisionEnter。例子:

private void OnCollisionEnter(Collision collision)
{
    transform.position = new Vector3(0f, 2f, 0f);
}

推荐阅读