Search Unity

AddRelativeForce not working correctly

Discussion in 'Scripting' started by Den D., Jul 26, 2014.

  1. Den D.

    Den D.

    Joined:
    May 30, 2014
    Posts:
    11
    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

    Code (CSharp):
    1.  /*  Part1
    2.      *Step Horizontal Movement
    3.      * Use to get left and right movement
    4.      */
    5.     if(Input.GetKey ("right")||Input.GetKey ("left")){
    6.         moveDir.x = Input.GetAxis("Horizontal"); // get result of AD keys in X
    7.     }
    8.     rigidbody.velocity = moveDir * speed;
    9.     /*Part2
    10.      *Continuse Forward Movement
    11.      */
    12.     if (Input.GetKey(KeyCode.UpArrow) || Input.GetKey(KeyCode.W))
    13.     {
    14.         moveDir.z = Input.GetAxis("Vertical");
    15.         rigidbody.AddRelativeForce(moveDir*50);
    16.     }
    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
     
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    You are setting the velocity and adding force. That is bound to give problems.
     
  3. Den D.

    Den D.

    Joined:
    May 30, 2014
    Posts:
    11
    So is there any other way to get the same result
     
  4. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
  5. Den D.

    Den D.

    Joined:
    May 30, 2014
    Posts:
    11
    This is what is used to implement using transform without rigidbody and gave pretty good result so I tried to get the same by using physic but all efforts were in vein

    Code (CSharp):
    1.    
    2.  Vector3 moveDir = Vector3.zero;
    3.         if(Input.GetKey ("right")||Input.GetKey ("left")){
    4.             moveDir.x = Input.GetAxis("Horizontal"); // get result of AD keys in X
    5.            
    6.             if(ShipCurrentSpeed==0)
    7.             {
    8.                 moveDir.x *= 3f * Time.deltaTime;
    9.                 //transform.position += moveDir * 3f * Time.deltaTime;
    10.             }else if(ShipCurrentSpeed<=15)
    11.             {
    12.                 moveDir *= ShipCurrentSpeed * 2f * Time.deltaTime;
    13.                 //transform.position += moveDir * ShipCurrentSpeed * 2f * Time.deltaTime;
    14.             }else
    15.             {
    16.                 moveDir *= 20f * Time.deltaTime;
    17.                 //transform.position += moveDir * 20f * Time.deltaTime;
    18.             }
    19.         }
    20.     //Increase inSpeed
    21.         if (Input.GetKey ("up") && (ShipCurrentSpeed < ShipTopSpeed)) {
    22.             ShipCurrentSpeed = ShipCurrentSpeed + ShipAcceleration;
    23.         }
    24.         //Decrese the speed
    25.         if (Input.GetKey ("down") && (ShipCurrentSpeed > RevShipTopSpeed)) {
    26.             ShipCurrentSpeed = ShipCurrentSpeed - ShipDecceleration;
    27.         }
    28.         if (ShipCurrentSpeed < 0 || ShipCurrentSpeed == 0) {
    29.             ShipCurrentSpeed = 0;
    30.             move = Vector3.forward * ShipCurrentSpeed;
    31.         }
    32.         if (ShipCurrentSpeed > 0) {
    33.             move = Vector3.forward * Time.deltaTime * ShipCurrentSpeed;
    34.         }
    35.         if(ShipCurrentSpeed > ShipTopSpeed)
    36.         {
    37.             ShipCurrentSpeed = ShipTopSpeed;
    38.         }
    39.  
    40.         //Jump
    41.         if (isGrounded) {
    42.             amountToJump = 0;
    43.                         // Jump
    44.             if (Input.GetButtonDown("Jump")) {
    45.                 amountToJump = jumpHeight;  
    46.             }
    47.         }
    48.  
    49.         amountToJump -= gravity * Time.deltaTime;
    50.         MovementPlayer(amountToJump * Time.deltaTime,move.z,moveDir.x);
    51.         //transform.Translate(move);
     
  6. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Code (csharp):
    1.  
    2. ShipCurrentSpeed = ShipCurrentSpeed + ShipAcceleration;
    3.  
    It is beter to make this time dependent:
    Code (csharp):
    1.  
    2. ShipCurrentSpeed += Time.deltaTime * ShipAcceleration;
    3.  
    This code does nothing.
    Code (csharp):
    1.  
    2. if (ShipCurrentSpeed < 0 || ShipCurrentSpeed == 0) {
    3.           ShipCurrentSpeed = 0;
    4.             move = Vector3.forward * ShipCurrentSpeed;
    5.        }
    6.  
    If you want to move something, in my opinion it is best to use Vector3.MoveTowards or AddRelativeForce or adjust the velocity if you know how it works. You should keep this in Update or a fixed Update and not a seperate coroutine. It is easiest to use the ships rotation to determine the direction to give the force in..

    Code (csharp):
    1.  
    2. move = Vector3.forward * Time.deltaTime * ShipCurrentSpeed;
    3.  
    Should be:
    Code (csharp):
    1.  
    2. rigidbody.AddRelativeForce (Vector3.forward * ShipCurrentSpeed);
    3.