0

我有以下问题。我正在尝试制作具有自上而下视图的游戏(类似于第一个 GTA 游戏)。问题是当我按下我的播放器正在移动但无法停止的键时。在这里你可以看到我的脚本:

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

public class PlayerMovemenet : MonoBehaviour
{
    private Rigidbody2D m_Rigidbody;
    private Transform playerTransform;
    public float m_Speed = 100.0f;
  

    // Start is called before the first frame update

    void Start()
    {
        m_Rigidbody = GetComponent<Rigidbody2D>();
        playerTransform = GameObject.Find("Player").transform;
        
    }

    // Update is called once per frame
    void FixedUpdate()
    {

        Vector3 playerPos = playerTransform.eulerAngles;

        if (Input.GetKey(KeyCode.UpArrow))
        {   
            m_Rigidbody.velocity= transform.up * Time.deltaTime * m_Speed;
            
        }



            if (Input.GetKey(KeyCode.DownArrow))
            {
                
                m_Rigidbody.velocity = transform.up * Time.deltaTime * (-m_Speed);
            
            }



            if (Input.GetKey(KeyCode.RightArrow))
            {
                 transform.Rotate(0, 0, -1 * m_Speed * Time.deltaTime);
            }

        if (Input.GetKey(KeyCode.LeftArrow))
        {
            transform.Rotate(0, 0, 1 * m_Speed * Time.deltaTime);

        }
        }


    }

'''

你能告诉我如何解决吗?感谢您的回答。

4

1 回答 1

0

请注意,通常速度已经是每秒绝对单位的值,在这种情况下您不想乘以Time.deltaTime

同样,一般来说,每当处理物理时,我根本不会使用transform,而是通过Rigidbody.

然后只是当你不按键时

if (Input.GetKey(KeyCode.UpArrow))
{   
    m_Rigidbody.velocity = Quaternion.Euler(0, 0, m_Rigidbody.rotation) * Vector3.up * m_Speed;
}
else if (Input.GetKey(KeyCode.DownArrow))
{           
    m_Rigidbody.velocity = Quaternion.Euler(0, 0, m_Rigidbody.rotation) * Vector3.down * m_Speed;
}
else
{
    m_Rigidbody.velocity = Vector2.zero;
}

对于轮换我也宁愿去

m_Rigidbody.rotation += m_Speed * Time.deltaTime;

m_Rigidbody.rotation -= m_Speed * Time.deltaTime;
于 2021-12-28T19:07:28.027 回答