[英]How to move 2D Object with WASD in Unity
我下面的代码仅适用于水平移动。 垂直运动不应该也起作用吗? 我刚刚开始学习基本的 2D Unity 编程:
public class Player : MonoBehaviour {
//These fields will be exposed to Unity so the dev can set the parameters there
[SerializeField] private float speed = 1f;
[SerializeField] private float upY;
[SerializeField] private float downY;
[SerializeField] private float leftX;
[SerializeField] private float rightX;
private Transform _transformY;
private Transform _transformX;
private Vector2 _currentPosY;
private Vector2 _currentPosX;
// Use this for initialization
void Start () {
_transformY = gameObject.GetComponent<Transform> ();
_currentPosY = _transformY.position;
_transformX = gameObject.GetComponent<Transform> ();
_currentPosX = _transformX.position;
}
// Update is called once per frame
void Update () {
_currentPosY = _transformY.position;
_currentPosX = _transformX.position;
float userInputV = Input.GetAxis ("Vertical");
float userInputH = Input.GetAxis ("Horizontal");
if (userInputV < 0)
_currentPosY -= new Vector2 (0, speed);
if (userInputV > 0)
_currentPosY += new Vector2 (0, speed);
if (userInputH < 0)
_currentPosX -= new Vector2 (speed, 0);
if (userInputH > 0)
_currentPosX += new Vector2 (speed, 0);
CheckBoundary ();
_transformY.position = _currentPosY;
_transformX.position = _currentPosX;
}
private void CheckBoundary(){
if (_currentPosY.y < upY)
_currentPosY.y = upY;
if (_currentPosY.y > downY)
_currentPosY.y = downY;
if (_currentPosX.x < leftX)
_currentPosX.x = leftX;
if (_currentPosX.x > rightX)
_currentPosX.x = rightX;
}
}
如果我删除/注释掉_currentPosX
及其相关代码,那么我的垂直移动就会起作用。 但是,如果我删除/注释掉_currentPosY
及其相关代码,那么我的水平移动就会起作用。
但是为什么我无法让它们同时工作呢? 我想我只是错过了一些东西,但我无法弄清楚,因为我只是这方面的初学者。
感谢哪位能给点建议。
编辑:为了进一步澄清......
我正在编写一个简单的 2d 游戏,让玩家使用 WASD 键在 4 个方向上移动。
W = move up
A = move left
S = move down
D = move right
我的主要问题是我只能让两个键在一个轴上工作:A 和 D 用于水平移动,而 W 和 S 根本不适用于垂直移动,反之亦然。
你不需要那些if
语句。 只需使用+=
将输入附加到当前变换位置。
无Rigidbody
移动:
public float speed = 100;
public Transform obj;
public void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 tempVect = new Vector3(h, v, 0);
tempVect = tempVect.normalized * speed * Time.deltaTime;
obj.transform.position += tempVect;
}
使用Rigidbody2D
移动对象:
public float speed = 100;
public Rigidbody2D rb;
public void Update()
{
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
Vector3 tempVect = new Vector3(h, v, 0);
tempVect = tempVect.normalized * speed * Time.deltaTime;
rb.MovePosition(rb.transform.position + tempVect);
}
如果您希望稍后能够检测到碰撞,我建议使用第二个代码并移动 Rigidbody。
注意:
您必须指定要移动到编辑器中的obj
插槽的obj
。 如果使用第二个代码,请将具有Rigidbody2D
的对象分配给编辑器中的rb
插槽。
此代码 100% 有效(您必须尝试一下。)
public float moveSpeed = 5;
void Start()
{
}
void Update()
{
if (Input.GetKey(KeyCode.D))
{
transform.position += Vector3.right * moveSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.A))
{
transform.position += Vector3.right * -moveSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.W))
{
transform.position += Vector3.up * moveSpeed * Time.deltaTime;
}
else if (Input.GetKey(KeyCode.S))
{
transform.position += Vector3.up * -moveSpeed * Time.deltaTime;
}
}
尝试将 Vector2 函数中的值 0 更改为当前的 x/y 位置......我的项目遇到了类似的问题
if (userInputV < 0)
_currentPosY -= new Vector2 (/*current position*/, speed);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.