- Home /
GameObejct moves slower after reloading scene
have wall, with a rigidbody, that moves on the z-axis constantly. To do this movement I am using the "rb.velocity" function. When the player, who is using a character controller, collides with this wall, the scene reloads. But my problem is that the wall moves faster on the first go and when the scene reloads it moves way slower. I have a constant speed for the wall that is applied with "void Update()" and it shouldn't change.
The thing is, when I build and run the game the wall has the same speed every reloaded scene. But in the editor, it doesn't. And when I build and run, the wall moves slower than on the first go in the editor, but faster than after the scene is reloaded in the editor.
Moving wall script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Wall : MonoBehaviour
{
public Rigidbody rb;
public float wallSpeed = 500f;
Vector3 movement;
void Start()
{
movement = new Vector3(0f, 0f, wallSpeed * Time.deltaTime);
}
void Update()
{
rb.velocity = movement;
}
}
Reload scene script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class ReloadScene : MonoBehaviour
{
public Transform playerTransform;
private void OnCollisionEnter(Collision collision)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
private void Update()
{
if (playerTransform.position.y < -10f)
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
}
I haven't found any fixes for this, so any help would be appreciated!
Answer by rh_galaxy · Apr 27 at 10:53 PM
It's because you set movement in Start() once, and the first time Time.deltaTime has one value, and the next time another value, resulting in different speeds.
Set movement once to a constant value instead, like this:
void Start()
{
movement = new Vector3(0f, 0f, wallSpeed * 0.016);
rb.velocity = movement;
}