Torque speed/intensity changing with screen size

I didn’t notice this until I maximized the game window to record a playthrough. I worked on the design in the split screen layout shown in the instruction videos, so with a relatively small game window. It was this size when I did created the scripts and did the original tuning to get the player control felling how I liked. When I maximized the game window, the torque was spinning much faster almost out of control like when we first wrote the script. I re-tuned it to make the playthrough video with maximized game widow, but then when I went back to my default workspace, the torque was then much slower in the small game window.

1 Like

Hi,

How do you apply the torque and where?

Theoretically, this problem should not occur if you use a Rigidbody2D method inside the FixedUpdate method. The latter is a Unity method like Update. You must not use Time.deltaTime there. Feel free to test that.


See also:

1 Like

This is is the snowboarder level of Complete C# Unity Game Developer 2D.
I don’t have much experience coding, so I’m not sure the best way to answer the question. The torque is part of the player controller script and uses a Rigidbody2D method. The code looks like this;

using System;

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

public class PlayerController : MonoBehaviour

{

[SerializeField] float torqueAmount = 1f;

[SerializeField] float boostSpeed = 30f;

[SerializeField] float baseSpeed = 20f;

Rigidbody2D rb2d;

SurfaceEffector2D surfaceEffector2D;

// Start is called before the first frame update

void Start()

{

    rb2d = GetComponent<Rigidbody2D>();

    surfaceEffector2D = FindObjectOfType<SurfaceEffector2D>();

}

// Update is called once per frame

void Update()

{

    RotatePlayer();

    RespondToBoost();

}

void RespondToBoost()

{

    //if we push up, then speed up

    if (Input.GetKey(KeyCode.UpArrow))

    {

        surfaceEffector2D.speed = boostSpeed;

    }

     else

    // otherwise stay at normal speed

    {

        surfaceEffector2D.speed = baseSpeed;

    }

           

}

private void RotatePlayer()

{

    if (Input.GetKey(KeyCode.LeftArrow))

    {

        rb2d.AddTorque(torqueAmount);

    }

    else if (Input.GetKey(KeyCode.RightArrow))

    {

        rb2d.AddTorque(-torqueAmount);

    }

}

}

First, when you work with the scene view open, the frame rate is affected by that. The scene view significantly affects the fps. This brings us to the second point: The frame rate is affecting your torque because it uses physics, and should be executed in FixedUpdate, not Update (like @Nina mentioned).

Create a new method named FixedUpdate. Remove RotatePlayer(); from Update and add it to the FixedUpdate method. Test your game. Did this fix the problem?

1 Like

This worked, except instead of making a new method I changed the existing one from Update to FixedUpdate. This did what I want, hopefully it doesn’t effect anything else in that method that I am not considering.

In this game, you probably won’t get any issues.

However, keep in mind that Update is for everything that does not have anything to do with a rigidbody (= the physics simulation). And the FixedUpdate method is for rigidbody things only. Never execute something like Input.GetKeyDown in FixedUpdate because, otherwise, your code might miss user input. FixedUpdate is somewhat “dangerous” in this respect.

1 Like

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

Privacy & Terms