When I Restart The game doesn't work properly

I have a problem with restarting the game. My demo educational game working properly but after the restart game not working good for example jump not work. The speed getting lower

play my game : SIMMER.io

player scprit:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//using UnityEngine.InputSystem;
using TMPro;

public class PlayerController : MonoBehaviour
{

private Rigidbody playerRB;
private float moveX, moveY, moveZ, yatay, dikey, yeksen;
private int count;
public TextMeshProUGUI score, zaman, can, durum;

public GameObject Powerobject, powerUpIndicator, winindicator, isongroundtest;
public bool GucAldi, isOnGround = true, gameover;
public ParticleSystem PlayerPowerUp;
private AudioSource playerAudio;
public AudioClip gemSound, JumpSound, YouWinSound, YouLoseSound, HurrySound, GetPowerUpSound, TeleportedSound;
[SerializeField] int cansayaci;
[SerializeField] private float zamansayaci = 10, tophizi = 5, AngularSpeed, GravityModifier = 10, JumpForce = 2000;
public Material GoldMetarial;
public Renderer Object;
public UnityEngine.UI.Button btn;

// public UnityEngine.UI.Text text;

//private InputActionAsset inputActions; new input için
//Keyboard keyboard;

private void Awake()
{
playerRB = GetComponent();
count = 0;
SetScoreText();
playerAudio = GetComponent();
Physics.gravity *= GravityModifier;

}

void Start()
{

}

private void Update()
{
winindicator.transform.position = transform.position + new Vector3(0, -0.5f, 0);
powerUpIndicator.transform.position = transform.position + new Vector3(0, 0.1f, 0);

if (!gameover)
{
zamansayaci -= Time.deltaTime;
zaman.text = “Time :” + " " + (int)zamansayaci + " "; // float int e ve sonra string e çevriliyor.
}

if (zamansayaci < 0 && !gameover)
{
gameover = true;
durum.text = “You lost”;
btn.gameObject.SetActive(true);
playerAudio.PlayOneShot(YouLoseSound, 0.5f);
Debug.Log(“Game over çaldı”);
}
if (transform.position.y < -45)
{
durum.text = “You lost”;

btn.gameObject.SetActive(true);
playerAudio.PlayOneShot(YouLoseSound, 0.060f);
gameover = true;
// Debug.Log(“Game over çaldı”);
}
}
void SetScoreText()
{
score.text = "Score : " + count.ToString();
if (count >= 15)
{
gameover = true;
winindicator.SetActive(true);
powerUpIndicator.SetActive(false);
durum.text = “You Win”;
btn.gameObject.SetActive(true);
playerAudio.PlayOneShot(YouWinSound, 0.5f);

}

}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag(“Ground”))
{
isOnGround = true;
isongroundtest.SetActive(true);
}
}

private void OnTriggerEnter(Collider other) // collision = cls
{
if (other.gameObject.CompareTag(“pickup”))
{
playerAudio.PlayOneShot(gemSound, 0.5f);
zamansayaci += 3;
count++;
SetScoreText();
Debug.Log(count);
Destroy(other.gameObject);
}

else if (other.gameObject.CompareTag(“Powerobject”))
{
playerAudio.PlayOneShot(GetPowerUpSound, 0.5f);
powerUpIndicator.SetActive(true);
Destroy(other.gameObject);
Object.GetComponent().material = GoldMetarial;
// playerAudio.PlayOneShot(GetPowerUpSound, 1.0f);
GucAldi = true;
tophizi = 70;
}
else if (other.gameObject.CompareTag(“Teleport”))
{
playerAudio.PlayOneShot(TeleportedSound, 0.5f);
transform.position = new Vector3(0, 2.37f, -18);
Debug.Log(“teleported”);
}
}
void FixedUpdate()
{

//Vector3 movement = new Vector3(moveX, 0.0f, moveY); NEW İNPUT SYSTEM
if (!gameover)
{
yatay = Input.GetAxis(“Horizontal”);
dikey = Input.GetAxis(“Vertical”);

Vector3 kuvvet = new Vector3(yatay, 0, dikey);

playerRB.AddForce(kuvvet * tophizi, ForceMode.Impulse);

}

if (Input.GetKeyDown(KeyCode.Space) && isOnGround) // çok güzel bir bool örneği switch mantığı ile kod bloğunu aktif ya da pasif ediyor.
{
playerRB.AddForce(Vector3.up * JumpForce, ForceMode.Impulse);
playerAudio.PlayOneShot(JumpSound, 0.5f);
isOnGround = false;
isongroundtest.SetActive(false);
Debug.Log(“SPACE BASILDI”);
}
else if (gameover)

{
playerRB.velocity = Vector3.zero;
playerRB.angularVelocity = Vector3.zero;
}

}

}
Restart button scprit

using System.Collections;
using System.Collections.Generic;

using UnityEngine;
using UnityEngine.SceneManagement;

public class Buttons : MonoBehaviour
{

public void restrat()
{

SceneManager.LoadScene(“Level1”);
}
}

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

How to understand compiler and other errors and even fix them yourself:

https://discussions.unity.com/t/824586/8

Beyond that, to isolate WHERE in the code you’re having an issue and just to help gain more insight into your problem in general, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

Unity debugger does not show any errors.
There is no mistake
There is no bouncing after the scene reloads. At the same time, the speed of the sphere is slowing down.

Glad to hear you’re back running.

If on the other hand you’re not, be sure to let us know what you find out when you do this:

1 Like

Your code is full of framerate-sensitive stuff like this:

winindicator.transform.position = transform.position + new Vector3(0, -0.5f, 0);
powerUpIndicator.transform.position = transform.position + new Vector3(0, 0.1f, 0);

This will behave differently at different framerates. You will have to go back and make it framerate independent if you want your objects to always move at the same speed.

You are also making another common mistake of trying to read input in FixedUpdate, for example:

Input.GetKeyDown(KeyCode.Space)

In short, you are falling into some common pitfalls.

  • Use Time.deltaTime in Update to make motion etc… framerate independent
  • Move input handling into Update so you don’t miss any inputs.