Unity3d(C#)玩家无法使用新的Vector3进行传送

3ks5zfa0  于 2022-11-15  发布在  C#
关注(0)|答案(1)|浏览(154)

每当我的玩家触摸名为“LevelEnd 1”的对象时,我希望它将玩家传送到一组特定的坐标并向控制台发送消息,但我的代码所做的只是向控制台发送消息。(没有错误/我将GameObject变量设置为Player)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerTouched : MonoBehaviour
{
    public GameObject Player;

    public void OnControllerColliderHit(ControllerColliderHit collision)
    {
        if (collision.gameObject.tag == "LevelEnd1")
        {
            Player.transform.position = new Vector3(-193.5f, 5.860001f, 803.13f);
            Debug.Log("it worked!!");
        }
    }
}
wwtsj6pe

wwtsj6pe1#

问题是我的移动脚本不断地改变我的位置,所以我在传送之前禁用了我的球员0.1秒的移动。
玩家触摸代码:

public void OnControllerColliderHit(ControllerColliderHit collision)
    {
        if (collision.gameObject.tag == "LevelEnd1")
        {
            StartCoroutine("Teleport");
        }
    }

    IEnumerator Teleport()
    {
        playerMovement.disabled = true;
        yield return new WaitForSeconds(0.1f);
        Player.transform.position = new Vector3(-193.5f, 5.860001f, 803.13f);
        Debug.Log("it worked!!");
        yield return new WaitForSeconds(0.1f);
        playerMovement.disabled = false;
    }

玩家移动代码:

public bool disabled = false;

    Vector3 velocity;
    bool isGrounded;

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

        if (isGrounded && velocity.y < 0 && !disabled)
        {
            velocity.y = -2f;
        }

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

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

        if (move.magnitude > 1 && !disabled)
            move /= move.magnitude;

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

        velocity.y += gravity * Time.deltaTime;

        controller.Move(velocity * Time.deltaTime);
    }

相关问题