Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by eismann33 · May 19, 2015 at 08:50 AM · rigidbodyjumptimescalefallingslowmotion

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges