简体   繁体   English

Unity3D:如何在按1键然后同时按另一个键时改变力?

[英]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.

相关问题 如何通过重复按一个键将 Unity3D 中的 object 从一个地方移动到另一个地方,并按设定的顺序进一步移动? - how can I move an object in Unity3D from one place to another and further in a set order by repeatedly pressing a single key? 通过按键/或鼠标单击 Unity3D 为 object 设置动画 - Animate an object by pressing Key / or Mouse Click Unity3D 我想在 unity3d c# 中按下一个键来冻结一个对象 - I want to freeze an object on pressing a key in unity3d c# 通过在Unity3d中按Vuforia虚拟按钮无法旋转多维数据集 - The cube does not rotate by pressing Vuforia virtual button in Unity3d 按下按钮时跳转(unity 2D) - Jump when pressing a button (unity 2D) 如何让按下键盘上的一个键就像按下另一个键? - How to make pressing a key on a keyboard is like pressing another key? UNITY3D如何在同一对象上同时执行多个动画? - UNITY3D How to do multiple animations on the same object at the same time? 如何在按下W键时使角色行走并在停止键时闲置/接地? - How can i make character walking when pressing W key and idle/grounded when stop pressing? 如何检查玩家是按住空格键还是按下空格键? - How do I check whether a player is holding the SPACE key or pressing it? Unity 向上按时读左键为右键 - Unity reading the left key as the right key when pressing up
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM