Any way to make the Rigidbody keep it's last known velocity after a jump?
Hello everyone! I'm making a FPS game where my character moves at high speeds an can jump around, but there's an issue.
Th eplayer has a Rigidbody, a Capsule Collider and two scripts.
The issue is the following: When i jump, i want the velocity of my Rigidbody to stay the same even if i look around. Do you think i'll be forced to put the camera outside of my prefab or is there a solution that would allow me to get the last known velocity at the moment of the jump or something / override the transform.forward velocity while in air and replacing it by another ( i'm a total newbie with code so maybe i'm not seeing something very obvious here)?
Here are my scripts (made using Brackey's tutorial on the online multiplayer fps) :
Script 1 (attached to the player prefab which hosts all the colliders and scripts :
And here's the second, which executes all commands from the first and is automatically attached to the prefab :
Answer by tormentoarmagedoom · Jan 15, 2019 at 04:39 PM
Good day.
You can just store the velocity in a Vector3 (or 2) variable the moment of the jump, and apply it once it touch the groud again.
Your question is really very simple, you should take a look at some other tutorials about movement and rigidbody, I'm sure you will get in very soon by your own!
Good luck! Bye!
@tormentoarmagedoom Thanks for the answer but i what do you mean by applying velocity when it touches the ground again?... I'm not sure i'm understanding what you mean. $$anonymous$$aybe i didn't write my question right : imagine my player jumping in one direction (let's say Z), then he moves his camera. The problem here is that my script makes the rigidbody rotate around the vertical axis (turning around) and the camera only around the horizontal axis (looking up and down). But i don't want my rigidbody's velocity to stay in transform.forward mode when i'm in the air.
Am i forced to make a separate script for the camera and never make the rigidbody turn is there any other solution?
Good again.
I'm still not sure of what you want xd (my english is not very good) i did not read your codes (cant open the files) ,dont atach files, is better if u post the code here using the "Code Sampler Button".
As you say, you use transform.forward. This is a relative direction (depends on object rotation). If you pretend conserve the velocity, you need to do it in a global absolute coords. Every frame, the rigidboy knows its global absolute velocity, represented as a vector3 RigidBody.velocity This does not care about where the object is facing, or cameras or anything else.
If you read this value at the moment of jumping, you can then apply that velocity to the rigidboy again when you want, or apply only some opf the components of the vector.
for example, if charactter is moving (1,0,1) and then jumps (1,1,1) you can make something like this to conserve the direction of the jump:
rb = GetComponent(RigidBody);
float XcompBeforeJump = rb.velocity.x;
float ZcompBeforeJump = rb.velocity.z;
Then force the rb to follow this velocity:
rb.velocity = new Vector3 (XcompBeforeJump , rb.velocity.y, ZcompBeforeJump );
So, Y value will change (because the gravity) but X and Z compoentns will stay constant.
Bye!
Your answer
Follow this Question
Related Questions
Rigidbody AddForce(transform.forward) vs Rigidbody.AddRelativeForce(transform.forward) ? 1 Answer
Rigidbody velocity forward is Camera forward 1 Answer
C# keep previous velocity while jumping, previous solutions not working 0 Answers
controlles like leo fortune 0 Answers
How can I make my player jump faster but not higher? 3 Answers