- Home /
how to make a player jump without being affected by Time.TimeScale
im trying to make a game where the player can slow down time at will but remain in normal speed (like superman or the flash :P) and im having trouble getting the jumping physics correct, i tried using the code from http://answers.unity.com/answers/857994/view.html via
public float JumpVelocity;
public float JumpDampening = 0.1f;
void Jump()
{
JumpVelocity = 0.5f;
m_RigidBody.useGravity = false;
}
// Update is called once per frame
private void Update()
{
GroundCheck();
InteractRaycast();
// printfTools.Tools.fprintf(Debug.Log, "controller.isGrounded = %s, m_IsGrounded = %s", controller.isGrounded == true ? "true" : "false", m_IsGrounded == true ? "true" : "false");
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= MovementSpeed;
if (Input.GetButton("Jump")) Jump();
// printfTools.Tools.fprintf(Debug.Log, "moveDirection.y = jumpForce ( %G ) / TM.Matrix_Time ( %G ) = %G (%G)", jumpForce, TM.Matrix_Time, jumpForce / TM.Matrix_Time, moveDirection.y);
MovementSpeed = MovementSpeedDefault / TM.Matrix_Time;
}
verticalVelocity = moveDirection.y;
controller.Move(moveDirection * Time.fixedDeltaTime);
Vector3 pos = transform.position;
if (JumpVelocity != 0)
{
pos.y += JumpVelocity;
JumpVelocity -= JumpDampening;
if (JumpVelocity <= 0)
{
m_RigidBody.useGravity = true;
JumpVelocity = 0;
}
}
else
{
pos.y -= JumpDampening;
}
transform.position = pos;
}
but my character just no-clips through everything including the ground thus i cannot check if this is accurate and solves my problem or not as it just keeps falling and falling, however it seems to fall at the same speed regardless of whether it is slowed down or not which is what i want (as your perception of time should appear to not change until you actually compare yourself to an object that is affected by time thus to yourself you would not notice that you are falling faster or slower or moving faster or slower untill again you compare yourself to something which is not affected by time)
my previous code was
// Update is called once per frame
private void Update()
{
GroundCheck();
InteractRaycast();
// printfTools.Tools.fprintf(Debug.Log, "controller.isGrounded = %s, m_IsGrounded = %s", controller.isGrounded == true ? "true" : "false", m_IsGrounded == true ? "true" : "false");
if (controller.isGrounded)
{
moveDirection = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
moveDirection = transform.TransformDirection(moveDirection);
moveDirection *= MovementSpeed;
if (Input.GetButton("Jump")) moveDirection.y = jumpForce / TM.Matrix_Time;
// printfTools.Tools.fprintf(Debug.Log, "moveDirection.y = jumpForce ( %G ) / TM.Matrix_Time ( %G ) = %G (%G)", jumpForce, TM.Matrix_Time, jumpForce / TM.Matrix_Time, moveDirection.y);
MovementSpeed = MovementSpeedDefault / TM.Matrix_Time;
}
else
{
moveDirection.y -= (9.8f * Time.fixedUnscaledDeltaTime) / TM.Matrix_Time;
printfTools.Tools.fprintf(Debug.Log, "moveDirection.y = (9.8f * Time.fixedUnscaledDeltaTime ( %G )) ( %G ) / TM.Matrix_Time ( %G ) = %G (%G)", Time.fixedUnscaledDeltaTime, (9.8f * Time.fixedUnscaledDeltaTime), TM.Matrix_Time, (9.8f * Time.fixedUnscaledDeltaTime) / TM.Matrix_Time, moveDirection.y);
}
verticalVelocity = moveDirection.y;
controller.Move(moveDirection * Time.fixedDeltaTime);
}
wich works however is not accurate, for example,
if you slow down then jump then speed back up you end up jumping like 10 times higher
if you jump than slow down i think you jump shorter, im not sure
if you slow down you fall faster than you normally would
if you slow down, jump, allow yourself to fall, speed backup while falling, then you fall faster than you normally would
again this is dependant on Time.TimeScale
at the moment i can only rely on debug printf's and visual observation as i cannot time the jump times for differences, eg
time it takes from stand still on ground to peak of jump,
time it takes from peak of jump to stand still on ground again
One approach would be that you store your own time variable in a global class and all the classes/objects that are affected by time have their movement and actions script modified by that variable.
For example, if you have an enemy, then it's movement could be affected by the ** vector Global.yourTimeVariable.* You also have to apply this variable to each of their animations and all of their actions.
Answer by Cornelis-de-Jager · Dec 02, 2018 at 04:15 AM
In your jumping section use :
Time.fixedTime // over Time.TimeScale
Time.FixedDeltaTime // over Time.DeltaTime
Also for physics operations use:
FixedUpdate () // Over update
This forces unity to use actual real time passed instead of Engine Time
on which code? my current code that no clips everything? or my old code that does not jump correctly and where would i replace them, as i do not see Time.DeltaTime nor Time.TimeScale anywhere, note T$$anonymous$$.$$anonymous$$atrix_Time holds the current value of Time.TimeScale, but even than why would i eed to change Time.fixedTime ins$$anonymous$$d of Time.Timescale to slow everything down
Assets/Scripts/Player/PlayerControls/Time$$anonymous$$anager.cs(29,8): error CS0200: Property or indexer `UnityEngine.Time.fixedTime' cannot be assigned to (it is read-only)
this is Time$$anonymous$$anager.cs
using System;
using UnityEngine;
public class Time$$anonymous$$anager : $$anonymous$$onoBehaviour {
public float slowdownFactor = 0.005f;
public float $$anonymous$$atrix_Time = 0;
public float Time_Original = 0;
public bool $$anonymous$$atrix = false;
public int $$anonymous$$atrix_State = 0;
void Update ()
{
if ($$anonymous$$atrix_Time == 0) $$anonymous$$atrix_Time = Time.timeScale;
if (Time_Original == 0) Time_Original = Time.timeScale;
if (Input.GetButtonDown("$$anonymous$$atrix")) $$anonymous$$atrix = !$$anonymous$$atrix;
if($$anonymous$$atrix) {
if ($$anonymous$$atrix_State == 0 || $$anonymous$$atrix_State == 3) DoSlowDown();
if ($$anonymous$$atrix_State == 1) DoSlowmotion();
}
else DoSpeedUp();
Time.fixedDeltaTime = Time.timeScale * .02f;
}
public void DoSlowmotion ()
{
$$anonymous$$atrix_State = 2;
$$anonymous$$atrix_Time = slowdownFactor;
Time.timeScale = $$anonymous$$atrix_Time;
}
public void DoSpeedUp() {
$$anonymous$$atrix_State = 3;
$$anonymous$$atrix_Time += (1f / 1f) * Time.unscaledDeltaTime;
$$anonymous$$atrix_Time = $$anonymous$$athf.Clamp($$anonymous$$atrix_Time, 0f, 1f);
Time.timeScale = $$anonymous$$atrix_Time;
if ($$anonymous$$atrix_Time >= Time_Original) $$anonymous$$atrix_State = 0;
}
public void DoSlowDown() {
$$anonymous$$atrix_Time -= (1f / 1f) * Time.unscaledDeltaTime;
$$anonymous$$atrix_Time = $$anonymous$$athf.Clamp($$anonymous$$atrix_Time, 0f, 1f);
Time.timeScale = $$anonymous$$atrix_Time;
if ($$anonymous$$atrix_Time < slowdownFactor) $$anonymous$$atrix_State = 1;
}
}
fml it exceeds 3000 chars ._. this is Jumping Physics.cs https://bpaste.net/show/54347b9499e6
I think what he meant is that you should change all Time.TimeScale to Time.fixedTime and all Time.DeltaTime to Time.FixedDeltaTime. Also, change your Update() method to FixedUpdate()
Your answer
Follow this Question
Related Questions
Player Falls Much Slower While Moving 2 Answers
How to reduce speed while moving left and right in 2D platform 0 Answers
why I don't have in the butter one click player movement ? 1 Answer
Movement (left/right) and jumping = laggy motions.. please help! 0 Answers
Adding MidAir Movement to Default Third Person Character 1 Answer