Quantcast
Viewing all articles
Browse latest Browse all 146

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

Viewing all articles
Browse latest Browse all 146

Trending Articles