- Home /
Everything in slowmotion except player: How to adjust fall/jump speed of character accordingly?
Hi guys, in my game I want the player to be able to slow down time by pressing a button, upon release of the button it changes back to normal. The character itself shouldn't be affected by the slowmotion at all. I used Time.timeScale = 0.1 to slow down time and simply sped up the character's movement accordingly. This works pretty well, but there is one problem: I can't make jumping and falling feel the same as in normal speed. I tried playing around with AddForce() and velocity() of the character's rigidbody aswell as Mass and Drag but I can't make it work. My current method is to simply multiply the y-component of velocity by 10 to counteract the slowmotion but then it just scales infinitely as long as time is slowed and the character flies off the screen. This is my script attached to the character:
public class CharacterMovement : MonoBehaviour {
public float maxSpeed = 6.0f;
public bool facingRight = true;
public float moveDirection;
public float gazeDirection;
public float jumpSpeed = 800;
public bool grounded = false;
public Transform groundCheck;
public float groundRadius = 0.2f;
public LayerMask whatIsGround;
public bool timeFreezed = false;
public int equalizeTimeFreeze = 10;
void Awake(){
groundCheck = GameObject.Find("GroundCheck").transform;
}
// Use this for initialization --> fixed insteadof normal update function to ensure update everytime the physics are being updated
//to make sure no physics calculations are wrong
void FixedUpdate () {
float upVelocity = GetComponent<Rigidbody> ().velocity.y;
grounded = Physics2D.OverlapCircle (groundCheck.position, groundRadius, whatIsGround);
if(timeFreezed){
GetComponent<Rigidbody> ().velocity = new Vector2 (moveDirection * maxSpeed * equalizeTimeFreeze * -1, upVelocity);
}else{
GetComponent<Rigidbody> ().velocity = new Vector2 (moveDirection * maxSpeed, upVelocity);
}
if (gazeDirection > 0.0f && !facingRight) {
Flip();
}else if(gazeDirection < 0.0f && facingRight){
Flip();
}
}
// Update is called once per frame
void Update () {
//movement
moveDirection = Input.GetAxisRaw ("Horizontal");
//aiming
gazeDirection = Input.GetAxisRaw("RightStickX");
//jump
if(grounded && Input.GetButtonDown("Jump")){
GetComponent<Rigidbody>().AddForce(new Vector2(0,jumpSpeed));
}
if (Input.GetButtonDown ("SkillButton")) {
freezeTime ();
}
if (Input.GetButtonUp ("SkillButton")) {
unfreezeTime ();
}
}
void Flip(){
facingRight = !facingRight;
transform.Rotate (Vector3.up,180.0f, Space.World);
}
void unfreezeTime (){
Time.timeScale = 1.0f;
timeFreezed = false;
}
void freezeTime (){
Time.timeScale = 0.1f;
timeFreezed = true;
}
Thank you very much in advance.
Answer by DientesDeCheto · Jan 19, 2016 at 07:19 PM
Hi! I know it's a little late, but maybe for someone else the solution could work.
Physics.gravity = new Vector3(0, Physics.gravity.y/slowmo_index , 0); //set gravity where you want
public void do_slowmo( RigidBody rb)
{
rb.velocity /= slowmo_index; // slowmo_index should be trated as inversed Time.TimeScale, where 2f half velocity is.
}
public void prevent_from_slowmo( RigidBody rb) //Adding the Physics.gravity/slowmo_index to get normal gravity
{
rb.useGravity = false; //You have to disable temporary the gravity use, instead you got a more forced (downwards) movement
rb.AddForce( new Vector3(0, Physics.gravity / slowmo_index, 0); // I'm not shure if it gets added (+) or may you have to add full gravity
}
Note that it's just psydocode!
So that means, lets supose, your slowmotion is a quarter of speed, that means, velocity/4, so you have to divide all velocity components (x,y,z) by 4. With a quarter of the original velocity the object moves at quarter of speed, but the acceleration also has to be adjusted, and in this case the gravity hast to be adjusted by 4 also: gravity/4 and voilá, the object moves at slowmotion.
For objects which you don't want to move at slowmo speed, just set the normal speed and adjust the force as result of the original gravity resting the slowmo gravity (or adding full gravity, as I don't know if it's an add + operation) to prevent the object move with slowmo gravity!
Good Luck
Your answer
Follow this Question
Related Questions
Slow motion for Physics game 2 Answers
Make rigidbodies use my own custom timescale? 1 Answer
my object not falling 1 Answer
AddForce Jump Problem 3 Answers
How can I put Slow Motion using Rigidbody Controller 2 Answers