![](/img/trans.png)
[英]something wrong with rotation controls in 2d top down shooter in unity3d
[英]Unity 2D Top Down Shooter movement issue C#
我正在尝试在Unity中制作一个简单的2D自顶向下射击游戏。 我掌握了鼠标的基本动作和跟随方式,但是动作有问题。
每当您同时按下说上移和右移时,它都会按预期的那样沿对角线移动,但是当您松开上移键时,它会一直沿对角线移动到我希望其向右移动的位置。 我处理运动的代码是:
private void Update ()
{
if (Input.GetKey(Up)) {
Debug.Log("UP");
Vector3 velUp = rigidbody2D.velocity;
velUp.y = walkSpeed;
rigidbody2D.velocity = velUp;
}
else if (Input.GetKey(Down)) {
Vector3 velDown = rigidbody2D.velocity;
velDown.y = walkSpeed*-1;
rigidbody2D.velocity = velDown;
}
else if (Input.GetKey(Left)) {
Vector3 velLeft = rigidbody2D.velocity;
velLeft.x = walkSpeed*-1;
rigidbody2D.velocity = velLeft;
}
else if (Input.GetKey(Right)) {
Vector3 velRight = rigidbody2D.velocity;
velRight.x = walkSpeed;
rigidbody2D.velocity = velRight;
}
else {
Vector3 velStop = rigidbody2D.velocity;
velStop.x = 0;
velStop.y = 0;
rigidbody2D.velocity = velStop;
}
//rotation
Vector3 mousePos = Input.mousePosition;
Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
如何使运动表现出我所说的? 这样,它沿对角线移动,使运动看起来不明显。
任何帮助是极大的赞赏。 谢谢。
您从速度0开始,然后将所有运动方向加起来。 然后,您可以对向量进行归一化并将其缩放为您的移动速度。 否则,如果走对角线,则玩家移动得更快。
我还没有检查代码,但是可以这样做:
void Update () {
Vector3 vel = new Vector3();
if(Input.GetKey(Up)){
Debug.Log("UP");
Vector3 velUp = new Vector3();
// just use 1 to set the direction.
velUp.y = 1;
vel += velUp;
}
else if(Input.GetKey(Down)){
Vector3 velDown = new Vector3();
velDown.y = -1;
vel += velDown;
}
// no else here. Combinations of up/down and left/right are fine.
if(Input.GetKey(Left)){
Vector3 velLeft = new Vector3();
velLeft.x = -1;
vel += velLeft;
}
else if(Input.GetKey(Right)){
Vector3 velRight = new Vector3();
velRight.x = 1;
vel += velRight;
}
// check if player wants to move at all. Don't check exactly for 0 to avoid rounding errors
// (magnitude will be 0, 1 or sqrt(2) here)
if (vel.magnitude > 0.001) {
Vector3.Normalize(vel);
vel *= walkSpeed;
rigidbody2D.velocity = vel;
}
//rotation
Vector3 mousePos = Input.mousePosition;
Vector3 objectPos = Camera.main.WorldToScreenPoint (transform.position);
mousePos.x = mousePos.x - objectPos.x;
mousePos.y = mousePos.y - objectPos.y;
float angle = Mathf.Atan2(mousePos.y, mousePos.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(new Vector3(0, 0, angle));
}
关于Normalize
看看这个图像。
如果您只向上走或向右走,您将以速度1移动(我们随后将其乘以您想要的速度)。 但是,如果您对角行走,则行走的速度约为所需速度的1.4倍(绿色矢量)。 Normalize
可保持向量的方向不变,但长度为1(红色向量)(也称为“幅值”)。
在较老的射击游戏中,您可能会发现一个称为“兔子跳”的错误。 我不确定这是否是问题的根源,但我想是这样的。
关于vel.magnitude > 0.001
:
我们实际上想知道vel.magnitude > 0
。 但是vel.magnitude
是计算的结果,因此可能包含舍入误差。 如果使用浮点值,请始终牢记这一点。 进行检查本身是因为Normalize
方法需要除以大小,而除以零是邪恶的。 不知道是否Normalize
检查本身。
似乎只有在没有按下任何键的情况下,才迫使速度达到零。
我可能会建议增加一些检查时看到Input.GetKeyUp
是true
,并设置rigidbody2D.velocity
的x
或y
值0
,也许是这样的:
void Update () {
if (Input.GetKeyUp(Up) || Input.GetKeyUp(Down)) {
rigidbody2D.velocity.y = 0;
}
if (Input.GetKeyUp(Left) || Input.GetKeyUp(Right)) {
rigidbody2D.velocity.x = 0;
}
...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.