Search Unity

Can't get AddRelativeForce to work

Discussion in 'Scripting' started by W1zb1t, Apr 22, 2019.

  1. W1zb1t

    W1zb1t

    Joined:
    Apr 22, 2019
    Posts:
    7
    I'm trying to make a player controller script for a tank based 2d top down game. The tank will rotate just fine but will not move forward (or any direction) with the code I'm using. Can anyone tell me what's wrong with it?

    Cheers

    Code (CSharp):
    1. public byte speed = 70;
    2.     public Rigidbody2D rb;
    3.     // Start is called before the first frame update
    4.     void Start()
    5.     {
    6.         rb = GetComponent<Rigidbody2D>();
    7.     }
    8.  
    9.     // Update is called once per frame
    10.     void Update()
    11.     {
    12.  
    13.        
    14.         if (Input.GetKey("left"))
    15.             transform.Rotate(Vector3.forward * Time.deltaTime * speed);
    16.         else if (Input.GetKey("right"))
    17.             transform.Rotate(Vector3.back * Time.deltaTime * speed);
    18.  
    19.         if (Input.GetKey("up"))
    20.         {
    21.             rb.AddRelativeForce(Vector3.forward * Time.deltaTime * speed);
    22.         }
    23.  
    24.     }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Try increasing the value of speed. The force you're applying as written is going to be incredibly small.

    Also, add forces in FixedUpdate, not Update.
     
  3. W1zb1t

    W1zb1t

    Joined:
    Apr 22, 2019
    Posts:
    7
    Hi, I've moved the input checks to FixedUpdate and changed speed to 1000 just for testing, the tank still rotates just fine but does not move in any direction.
     
  4. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    No, do all input checks in Update. Move all forces to FixedUpdate.

    As your code is written above though, just do the math first. If your game is running at 120 FPS currently, that will result in a force of:

    0.0083 * 70 = 0.581

    With 1000 speed:
    0.0083 * 1000 = 8.3

    At 8.3 that might still not be large enough to overcome whatever friction your tank has with the terrain. If your FPS is even higher (with simple games this is common) then the force will be much smaller. That is because multiplying force against deltaTime means the faster the FPS the smaller the force you are applying. Move all forces to FixedUpdate still, and when you do that you no longer need to multiply against deltaTime anyways.

    There may well be a problem with your code still, but first try actually applying a large force before anything else.
     
  5. W1zb1t

    W1zb1t

    Joined:
    Apr 22, 2019
    Posts:
    7
    Ive done as you said, and changed the it to 10000000 and still the tank will turn but not move in any direction. There is no terrain its a 2d top down, the tank is just there on screen with nothing else, is that the problem? I've changed its drag properties and gravity to 0 (otherwise it just drops off screen) but still no effect.