Gravity and or torque amount is changing when the scene is reset

Not sure if this is a unity bug but after doing this lesson the gravity and or torque lowers substantially on scene reset and my player can flip around really fast. I’ve checked Ricks scripts and there identical not sure what happened and how to fix this.

Hi, you should probably post some code, that might help with solving your problem!

What’s the best way to post code

You can post it here, best if you put the code between 3 grave symbol top and 3 grave symbol below,
then you get this:

code here

Hi @BenCour,

Please follow this instruction: How to apply code formatting within your post

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;

    bool canMove = true;

    // Start is called before the first frame update

    void Start()

    {

       rb2d = GetComponent<Rigidbody2D>();

       surfaceEffector2D = FindObjectOfType<SurfaceEffector2D>();

    }

    // Update is called once per frame

    void Update()

    {

        if (canMove)

        {

            RotatePlayer();

            RespondToBoost();

        }

    }

    public void DisableControls()

    {

        canMove = false;

    }

   void RespondToBoost()

    {

       if (Input.GetKey(KeyCode.UpArrow))

       {

          surfaceEffector2D.speed = boostSpeed;

       }

       else

       {

           surfaceEffector2D.speed = baseSpeed;

       }

    }

    void RotatePlayer()

    {

        if (Input.GetKey(KeyCode.LeftArrow))

        {

            rb2d.AddTorque(torqueAmount);

        }

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

        {

            rb2d.AddTorque(-torqueAmount);

        }

    }

}

This script is where most changes were made so I imagine the error must be somewhere on this script

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CrashDetector : MonoBehaviour
{
   
  
 [SerializeField] float Delay = 1f;
 [SerializeField] ParticleSystem crashEffect;
 [SerializeField] AudioClip crashSFX;
 bool hasCrashed = false;

  void OnTriggerEnter2D(Collider2D other) 
  {
      if(other.tag == "Ground" && !hasCrashed)
      {
        hasCrashed = true;
        FindObjectOfType<PlayerController>().DisableControls();
        crashEffect.Play();
        GetComponent<AudioSource>().PlayOneShot(crashSFX);
        Invoke ("ReloadScene", Delay);
       
      }
  }

  void ReloadScene()
  {
      SceneManager.LoadScene(0);
  }


}

This script resets the scene when the player crashes so the issue could also be here

Seemed to fix it by changing Update to FixedUpdate. Is this a good fix to the issue or would it be better to have the movement be * Time.DeltaTime?

The whole post is a bit messy to follow / light on details, but usually, if changing it from update to fixedUpdate, you may be fine or you may have just been lucky and you’ll find you’ll have issues on machines with differing performance.

If you use a Rigidbody2D method, it’s perfectly fine to use it in FixedUpdate (without Time.deltaTime!). Since you use GetKey (instead of GetKeyDown), you could also check the user input in FixedUpdate if you don’t want to do that in Update.

Regarding the SurfaceEffector2D, I do not know if it is part of Unity’s physics simulation like the Rigidbody2D. If it works in FixedUpdate, feel free to use it there. Otherwise, use it in Update.

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

Privacy & Terms