Unity 中的自顶向下游戏。玩家无法停止移动
Top down game in Unity. Player can't stop moving
我有以下问题。我正在尝试制作具有自上而下视图的游戏(类似于第一款 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);
}
}
}
'''
你能告诉我如何解决吗?感谢您的回答。
请注意,通常速度已经是一个以每秒绝对单位为单位的值,在这种情况下您不想乘以 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;
我有以下问题。我正在尝试制作具有自上而下视图的游戏(类似于第一款 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);
}
}
}
'''
你能告诉我如何解决吗?感谢您的回答。
请注意,通常速度已经是一个以每秒绝对单位为单位的值,在这种情况下您不想乘以 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;