- Home /
Movement script stops working when time.timescale= 0
Hi! I've got a movement script which should work when the game is "paused" however it doesn't. Could someone help me figure out why it isn't working when Time.timeScale = 0f? I made sure not to multiply by Time.deltaTime
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteAlways]
public class Movement : MonoBehaviour
{
public Transform target;
public float movementSpeed;
public float minZoom;
public float maxZoom;
public float minHeight;
public float maxHeight;
public float objectCameraDistance;
public float rotateMultiplier = 0.1f;
float angleXZ = 0;
float height = 0;
void Awake()
{
GameObject gameObject = GameObject.Find("Look At");
target = gameObject.transform;
}
void Update()
{
//ESTABLISH INPUT + INPUT VECTOR
float horizontalInput = Input.GetAxis("Horizontal");
float verticalInput = Input.GetAxis("Vertical");
float zoomInput = Input.GetAxis("Mouse ScrollWheel");
Vector3 inputDirections = new Vector3(horizontalInput, verticalInput, zoomInput).normalized;
//ZOOM
objectCameraDistance += -inputDirections.z * movementSpeed;
objectCameraDistance = Mathf.Clamp(objectCameraDistance, minZoom, maxZoom);
rotateMultiplier = (rotateMultiplier * objectCameraDistance) / objectCameraDistance;
//transform.position = new Vector3(0, 0, objectCameraDistance);
//HORIZONTAL ROTATION
angleXZ += inputDirections.x * movementSpeed * rotateMultiplier;
float positionX = Mathf.Cos(angleXZ) * objectCameraDistance;
float positionZ = Mathf.Sin(angleXZ) * objectCameraDistance;
//VERTICAL
height += inputDirections.y * movementSpeed;
height = Mathf.Clamp(height, minHeight, maxHeight);
//TRANSFORM
transform.position = new Vector3(positionX, height, positionZ);
transform.LookAt(target);
}
}
Answer by pauldarius98 · Feb 26, 2021 at 02:30 PM
Update Answer: Input.GetAxis uses Time.deltaTime to smooth the values and when the delta time is 0 the value won't change. Use GetAxisRaw instead but the Sensitivity/Gravity of the axis won't be taken in consideration anymore so most likely you want to post process the axis values yourself
Hi Paul! Thanks for your answer, I'm making a sim kind of game. When the timeScale is set to 0, the player can observe the "playfield" as static and can move the camera around the static environment. I also read that the update method is called, even though time.timeScale = 0f
What should I do to run the coroutine as an update function?
My bad actually, the problem is in fact not that Update is not called (it is still called) but that Input.GetAxis actually uses delta time to smooth the values. You can use Input.GetAxisRaw but that will no longer take in account the Sensitivity/Gravity.