Unity - 我怎样才能修复这段代码,让它在没有 transform.position = newPosition 的情况下工作?

问题描述 投票:0回答:0

所以我的角色必须绕着塔走,很简单

但是我是个笨蛋,只能通过在更新中使用 transform.position 来解决这个问题,所以跳跃和碰撞是行不通的

这是塔的概念(见图) Tower

这是它的工作原理 rn(见视频:https://streamable.com/7tmq1l

如果我评论 transform.position = newposition 允许跳跃,你会在剪辑中看到它有多糟糕

这是我使用的代码:

public class Movement : MonoBehaviour
{
    [SerializeField] private float radius = 7;
    [SerializeField] private float angleSpeed = 28;
    [SerializeField] private float jumpForce = 5;
    private float angle;

    Rigidbody rb;

    private void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    void Update()
    {
        transform.rotation = Quaternion.Euler(0, angle, 0);
        float horizontalInput = Input.GetAxis("Horizontal");

        angle -= horizontalInput * angleSpeed * Time.deltaTime;

        Vector3 newPosition = Quaternion.Euler(0, angle, 0) * new Vector3(0, 0, radius);

        transform.position = newPosition;

        //jump
        if(Input.GetKeyDown(KeyCode.Space)|| Input.GetKeyDown(KeyCode.W))
        {
            Debug.Log("Jumping!");
            rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
        }
    }

Ofc因为这个跳跃不起作用

c# unity3d controller move
© www.soinside.com 2019 - 2024. All rights reserved.