[英]Unity3D: How do i change force when pressing 1 key then pressing another at the same time?
i want to simulate a simple thrust like effect for example (Lunar Land). 我想模拟一个简单的推力效果(例如“月球之地”)。 i want to thrust up when pressing up arrow key then thrust left/right when pressing the appropriate arrow key but still whilst keeping up pressed.
我想在按向上箭头键时向上推,然后在按相应的箭头键时向左/向右推,但仍要保持按住状态。
if (Input.GetKey(KeyCode.UpArrow))
{
thrust = (Vector3.up * thrustForce);
}
else if (Input.GetKey(KeyCode.LeftArrow))
{
thrust = (Vector3.left * thrustForce);
}
else if (Input.GetKey(KeyCode.RightArrow))
{
thrust = (Vector3.right * thrustForce);
}
else
{
thrust = (Vector3.zero);
}
this is what i started with, and then started to add multiple "IF" statements which still did not move right. 这是我开始的工作,然后开始添加多个“ IF”语句,但这些语句仍然无法正确执行。 basically below, when i press UP the object thrusts up OK, but will not move left\\right until i let go of UP and press left\\right again.
基本上在下面,当我按下UP时,对象会向上推,但直到我放开UP并再次按下left \\ right时,它才会向左\\右移动。
i know this is probably a simple code issue 我知道这可能是一个简单的代码问题
The reason this won't work, is that it will only execute ONE of the statements, since you have and else if. 之所以不起作用,是因为它只能执行其中一条语句,因为您必须执行,否则执行。 You need plain IFs like this:
您需要像这样的普通IF:
bool keyHold = false;
if (Input.GetKey(KeyCode.UpArrow))
{
thrust = (Vector3.up * thrustForce);
keyHold = true;
}
if (Input.GetKey(KeyCode.LeftArrow))
{
thrust = (Vector3.left * thrustForce);
keyHold = true;
}
if (Input.GetKey(KeyCode.RightArrow))
{
thrust = (Vector3.right * thrustForce);
keyHold = true;
}
if(!keyHold) {
thrust = (Vector3.zero);
}
This is a pretty simple fix. 这是一个非常简单的修复。 Your code currently goes inside the
if
for the up arrow when it's held, and because you use else if
s, the code doesn't even try to check for the other arrow keys. 当前,您的代码在被按住的
if
下位于向上箭头的if
内,并且由于您使用else if
,因此代码甚至不会尝试检查其他箭头键。 If you're not holding up, then it checks for left arrow, then right and so on, stopping if one of those keys are pressed and jumping over everything after. 如果您不按住,则先检查向左箭头,再检查向右,依此类推,如果按下其中一个键则停止,然后跳过所有内容。
In addition, because you're using =
on your thrust, the statements inside the ifs will just override previous stuff too, such that if you went into all ifs, only the last reached thrust will be applied, overwriting your previous sets. 另外,由于您在推力上使用了
=
,因此ifs中的语句也会覆盖先前的内容,因此,如果您进入所有ifs,则只会应用最后到达的推力,从而覆盖以前的设置。
Therefore, you have two minor issues: else if
s and using =
instead of adding on thrust. 因此,您有两个小问题:
else if
s和使用=
而不是加推力。 A possible fix would be something like: 可能的解决方法是:
thrust = Vector3.Zero; //Start with zero
if (Input.GetKey(KeyCode.UpArrow))
{
thrust += Vector3.up * thrustForce; //Add on up thrust
}
if (Input.GetKey(KeyCode.LeftArrow))
{
thrust += Vector3.left * thrustForce; //Add on left thrust
}
if (Input.GetKey(KeyCode.RightArrow))
{
thrust += Vector3.right * thrustForce; //Add on right thrust
}
And if you hold left and right simultaneously, they just cancel each other out without the need for any checks. 而且,如果您同时按住左右键,它们将互相抵消,而无需进行任何检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.