【问题标题】:How do I assign a new transform.position for a GameObject that's triggered by an OnTriggerEnter in Unity?如何为 Unity 中由 OnTriggerEnter 触发的 GameObject 分配新的 transform.position?
【发布时间】:2019-10-23 19:33:42
【问题描述】:

我的 PlayerScript 中有代码,当与地板游戏对象(我用“FloorTag”标记)发生碰撞时,会减少一个名为“Lives”的变量。生命在减少,所以我知道碰撞正在记录(并且所有内容都已正确标记)。但是,我还希望将 Player 游戏对象的位置重置为我在与地板碰撞时声明的特定 Vector3。但是,出于某种原因,它没有这样做。我哪里出错了?

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

public class PlayerScript : MonoBehaviour
{

    public int Lives;
    public Text LivesText;
    public int Points;
    public Text PointsText;
    public Text GameOverText;

    public CharacterController CharController;
    public float moveSpeed = 9;
    public float rotSpeed = 85;

    float yVelocity = 0;
    public float jumpForce = 2.5f;
    public float gravityModifier = 0.025f;
    bool prevIsGrounded;

    Vector3 StartingPlatformCoords = new Vector3 (38, 16, 38);

    // Start is called before the first frame update
    void Start()
    {
        Lives = 8;
        SetLivesText();
        Points = 0;
        SetPointsText();

        prevIsGrounded = CharController.isGrounded;
        //CharController = gameObject.GetComponent<CharacterController>();
    }

    // Update is called once per frame
    void Update()
    {
        // L-R Forward-Back Motion
        float hAxis = Input.GetAxis("Horizontal");
        float vAxis = Input.GetAxis("Vertical");

        transform.Rotate(0, rotSpeed * Time.deltaTime * hAxis, 0);
        Vector3 amountToMove = transform.forward * moveSpeed * Time.deltaTime * vAxis;

        // Jump Motion
        if (CharController.isGrounded)
        {
            if (!prevIsGrounded && CharController.isGrounded)
            {
                yVelocity = 0;
            }

            if (Input.GetKeyDown(KeyCode.Space))
            {
                yVelocity = jumpForce;
            }
        }
        else
        {
            if (Input.GetKeyUp(KeyCode.Space))
            {
                yVelocity = 0;
            }

            yVelocity += Physics.gravity.y * gravityModifier;
        }

        amountToMove.y = yVelocity;
        // Modify the y-value within this Vector3 (which contains an x, y, z, and some utility functions like distance etc.) manually

        // Final Motion
        CharController.Move(amountToMove);

        // Update
        prevIsGrounded = CharController.isGrounded;

        // Camera
        Vector3 camPos = transform.position + transform.forward * -10 + Vector3.up * 3;
        Camera.main.transform.position = camPos;
        Camera.main.transform.LookAt(transform);

        //if (Input.GetKeyDown(KeyCode.Space))
        //{
        //yVelocity = jumpForce;
        //}

        //yVelocity += Physics.gravity.y * gravityModifier;
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("FloorTag"))
        {
            transform.position = StartingPlatformCoords;
            Lives -= 1;
            SetLivesText();
            Debug.Log(transform.position);
        }

        if (other.gameObject.CompareTag("CellTag"))
        {
            Points += 1;
            SetPointsText();
        }
    }

    void SetLivesText()
    {
        LivesText.text = "Lives: " + Lives.ToString();
        if (Lives <= 0)
        {
            GameOverText.text = "Game Over";
        }
    }

    void SetPointsText()
    {
        PointsText.text = "Score: " + Points.ToString();
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    CharacterController 可以覆盖对transform.position 所做的更改。最简单的解决方法是禁用它,然后直接修改transform.position后重新启用它:

    public CharacterController CharController;
    
    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("FloorTag"))
        {
            CharController.enabled = false;
            transform.position = StartingPlatformCoords;
            CharController.enabled = true;
    
            Lives -= 1;
            SetLivesText();
            Debug.Log(transform.position);
        }
    
        if (other.gameObject.CompareTag("CellTag"))
        {
            Points += 1;
            SetPointsText();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-07-15
      • 1970-01-01
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多