![](/img/trans.png)
[英]Unity Why can't the player turn towards the cursor in a 3d top-down game? (only Y-axis)Perspective Camera/LookAt
[英]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
。
同样,一般来说,每当处理 Physics 时,我根本不会使用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;
}
对于旋转我也宁愿 go
m_Rigidbody.rotation += m_Speed * Time.deltaTime;
和
m_Rigidbody.rotation -= m_Speed * Time.deltaTime;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.