Weird Build Torque Issue

So, I finished the course. For all intents and purposes I learned the material, and can definitely move on to the next section, but I DO have an odd issue that I’m curious about finding a solution to, in case I run into similar issues post-project in future endeavors; In both my code, and in the inspector, my torque is set to 2.5. While playing in Unity, the character can complete 3-4 rotations on a large jump due to torque speed. However, once I build my project out and open it outside of Unity, the torque is DRASTICALLY reduced, to where I can complete 1, MAYBE 2 rotations on a large jump. Due to the differing number of rotations off of the same jump, I know that it isn’t a latency issue, and both my code and inspector values match, so what gives?

The code in question:

public class PlayerController : MonoBehaviour
{
    [SerializeField] float torqueAmount = 2.5f;
    [SerializeField] float boostSpeed = 30f;
    [SerializeField] float baseSpeed = 20f;
    Rigidbody2D rb2d;
    SurfaceEffector2D surfaceEffector2D;
    bool canMove = true;
    void Start()
    {
        rb2d = GetComponent<Rigidbody2D>();
        surfaceEffector2D = FindObjectOfType<SurfaceEffector2D>();
    }


    void Update()
    {
        if (canMove)
        RotatePlayer();
        RespondToBoost();
    
    }
    
    public void DisableControls()
    {
        canMove = false;
    }


    void RespondToBoost()
    {
        if (Input.GetKey(KeyCode.W))
        {
            surfaceEffector2D.speed = boostSpeed;
        }
        else
        {
            surfaceEffector2D.speed = baseSpeed;
        }
    }
        
        

    void RotatePlayer()
    {
        if (Input.GetKey(KeyCode.A))
        {
            rb2d.AddTorque(torqueAmount);
        }
        else if (Input.GetKey(KeyCode.D))
        {
            rb2d.AddTorque(-torqueAmount);
        }
    }
}

Thank you for any information.

Hi,

Where did you set the torqueAmount value? Just in your code? If so, check your Inspector because, once you created a PlayerController component in the Inspector, the Inspector controls said component and ignores any changes of the [SerializedField] values in your code.

As I said, the value is the same in BOTH the code AND the Inspector. The field is serialized.

Please try the following:

  • Add the FixedUpdate method to your code. It’s a Unity method like Update.
  • Remove RotatePlayer(); from Update and add it to FixedUpdate.
  • Save your script.
  • Test your game.

Did this fix your problem?

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms