Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

AddRelativeTorque on different platforms

Discussion in 'Scripting' started by kemalenver, Apr 16, 2020.

  1. kemalenver

    kemalenver

    Joined:
    Jul 2, 2019
    Posts:
    2
    Please bare with me I'm new to Unity! The following code works nicely in the player, but when I run it on an iPhone the torque is much greater than I would expect. I'm using Time.deltaTime so what am I missing? The intent is to move the car forwards at a max speed, and to allow A/D or left tap/right tap to add torque and turn.

    Code (CSharp):
    1. private void Update()
    2.     {
    3.         if (IsGrounded())
    4.         {
    5.             playerRb.AddRelativeForce(Vector3.forward * accel * Time.deltaTime, ForceMode.VelocityChange);
    6.             if (playerRb.velocity.magnitude > maxSpeed)
    7.             {
    8.                 playerRb.velocity = playerRb.velocity.normalized * maxSpeed;
    9.             }
    10.             // Player Controls
    11.             // Rotate us left
    12.             if (Input.GetKeyDown(KeyCode.A))
    13.             {
    14.                 playerRb.AddRelativeTorque(-Vector3.up * rotationFactor * Time.deltaTime, ForceMode.Acceleration);
    15.             }
    16.  
    17.             // Rotate us right
    18.             if (Input.GetKeyDown(KeyCode.D))
    19.             {
    20.                 playerRb.AddRelativeTorque(Vector3.up * rotationFactor * Time.deltaTime, ForceMode.Acceleration);
    21.             }
    22.  
    23.             // Phone Controls
    24.             if (Input.touches.Length > 0)
    25.             {
    26.                 // Left tap
    27.                 if(Input.touches[0].position.x < Screen.width / 2 )
    28.                 {
    29.                     playerRb.AddRelativeTorque(-Vector3.up * rotationFactor * Time.deltaTime, ForceMode.Acceleration);
    30.                 } else
    31.                 {
    32.                     playerRb.AddRelativeTorque(Vector3.up * rotationFactor * Time.deltaTime, ForceMode.Acceleration);
    33.                 }
    34.             }
    35.         }
    36.     }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Bunch of things:

    - Gather all the input in Update() as you are doing, but store it to temporary variables. Do not act on it yet.

    - Do all physics force and torque adds in FixedUpdate() based on the temporary variables you set above

    - Do not use Time.deltaTime at all. Just apply the force/torque when you want it. Unity will slice it according to how much time has gone by, which will be a constant slice in FixedUpdate()
     
    kemalenver likes this.
  3. kemalenver

    kemalenver

    Joined:
    Jul 2, 2019
    Posts:
    2
    Thanks Kurt that helped a lot.
     
    Kurt-Dekker likes this.