Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 smallville7123 · Dec 02, 2018 at 03:34 AM · movementjumpingtimescalerealisticaccuracy

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

Comment
Add comment · Show 1
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
avatar image $$anonymous$$ · Dec 02, 2018 at 04:06 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 13 · 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
avatar image smallville7123 · Dec 02, 2018 at 05:44 AM 0
Share

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

avatar image smallville7123 · Dec 02, 2018 at 05:52 AM 0
Share

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)

avatar image smallville7123 · Dec 02, 2018 at 05:58 AM 0
Share

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;
     }
 }
avatar image smallville7123 · Dec 02, 2018 at 06:02 AM 0
Share

fml it exceeds 3000 chars ._. this is Jumping Physics.cs https://bpaste.net/show/54347b9499e6

avatar image $$anonymous$$ smallville7123 · Dec 02, 2018 at 03:14 PM 0
Share

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()

avatar image smallville7123 $$anonymous$$ · Dec 03, 2018 at 05:54 AM 0
Share

i only have Time.unscaledDeltaTime

Show more comments
Show more comments

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

151 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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