I am stuck on the code for couple of days but couldn't figure it out. Therefore I turned out to look the answer here but couldn't find any that match the requirement Hope some One might help

here is the code

/*  Part1 
 *Step Horizontal Movement
 * Use to get left and right movement 
if(Input.GetKey ("right")||Input.GetKey ("left")){
    moveDir.x = Input.GetAxis("Horizontal"); // get result of AD keys in X
rigidbody.velocity = moveDir * speed;

 *Continuse Forward Movement
if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
    moveDir.z = Input.GetAxis("Vertical");

I have a Cube with added rigidBody Component. When pressed left or right key the cube should move in that direction and when key is released it should stop Part one when run commenting part2 code runs correctly

the other movement that I am trying to achieve is the cube should move continuously.But the thing is when pressed up key it should increase the magnitude and when released it should maintain the magnitude and move constantly.This is also achieved successfull with the part2 code by commenting part1 code But when but run without commenting any of them part1 results are correct but part2 does not provide correct result which was obtained by commenting part1 code

Is there any other way to obtain the same reult

  • 1
    \$\begingroup\$ Chances are AddRelativeForce is working correctly but either you're doing something wrong (misinterpreting the documentation, not understanding the assumptions, etc.). It's a pet peeve of mine when people blame the tools (especially when the tools are something that are used by a lot of people so they're pretty thoroughly tested) when it's much more likely that the fault lies in user code. \$\endgroup\$ – Tetrad Jul 26 '14 at 16:23

Your primary problem is when you modify the velocity directly:

rigidbody.velocity = moveDir * speed;

Here, your moveDir variable is only getting its x value set, so the z value is zero. That means that whatever you did to change the velocity later on in the code, gets erased the very next frame when you use the above line of code.

You can either not alter the velocity directly, by using forces to move left/right, or you can make sure you include the existing forward velocity when changing the left/right velocity. Something like:

Vector3 newVelocity = moveDir * speed;
newVelocity.z = rigidBody.velocity.z;
rigidbody.velocity = newVelocity;
  • \$\begingroup\$ But connot obtain the same result I want forwand movement that is z axis to be continouse and the left right movement should be done only once when pressed key and should stop left/right movement when key is released \$\endgroup\$ – Abdullah Sorathia Jul 26 '14 at 15:26
  • \$\begingroup\$ Yes, I assumed that, which is why I provided the second option with code sample. \$\endgroup\$ – MichaelHouse Jul 26 '14 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.