- Home /
Slowing down a fall from a Jump
Does anyone know how to slow down a character when falling down? Here's the movement code I'm using:
private var jumpSpeed : float = 12.0; private var gravity = 100.0; private var moveDirection : Vector3 = Vector3.zero; private var charController : CharacterController; function Update () { if(charController.isGrounded == true) {
if(Input.GetButtonDown("Jump")){
moveDirection.y = jumpSpeed;
//audio.PlayOneShot(Jump);
}
}
moveDirection.y -= gravity * Time.deltaTime;
charController.Move(moveDirection * (Time.deltaTime * walkSpeed));
transform.position.z = 0.0;
}
What I mean is that the player jumps and lands too fast for the player to make accurate movements while trying to jump on a platform.
Answer by Persona · Dec 14, 2010 at 11:04 PM
Never mind. I got it now by playing with the variables.
Answer by Proclyon · Nov 16, 2010 at 12:32 PM
You should split your editing into parts
if horizontal move --> moveDirection = new Vector3(moveDirection.x + changeValue in x, moveDirection.y, moveDirection.z);
if B vertical move --> change moveDirection vector = new Vector3(moveDirection.x, moveDirection.y + changeValue in y*/, moveDirection.z);
if C --> change moveDirection vector = new Vector3(moveDirection.x, moveDirection.y, moveDirection.z + changeValue in z);
Splitting the logic like this can help you keep track of who is doing what when (The flow of the code)
The problem is that only JS will let you change the existing x .y .z. It's not the perfect solution since u keep reconstructing and editing exsiting values. But it demonstrates that you can maintain good logic in the code.
To fall down slower now looks like a simple solution.
Change the y value in the B example on the event when u want it and keep doing that with a small number untill you want to stop. The stop condition can be a clock , a max speed value or user input. Whatever you like
EDIT 11/22/2010 22:45:
Make a variable , sample name V
//Initial value is (0.0f, 0.0f, 0.0f) private Vector3 V = new Vector3.zero; private float jumpHeight = 10.0f;
//Null object private CharacterController c;
//Now to get the motion you want into the V vector with destroying previous changes V = new Vector3(V.x, V.x + jumpHeight, V.z);
//Get the character controller on this object if(GetComponent<CharacterController>() != null) { //Set the (not null by proof) component into the variable c = GetComponent<CharacterController>(); }
//Execute the move with the following unity function
//It demands a parameter so it knows WHERE to go
c.Move(V);
Sorry about all the C#. I don't really know where to start in JS except just putting var everywhere. Hope this helps out enough
Yeah, I can't see where you're going at here. No, I can see but I can't see how to code that.
Answer by StephanK · Nov 15, 2010 at 04:53 PM
What do you mean by slowing down? He will fall slower and slower and finally stop falling down? You have an error in your script, you are applying walkspeed to your gravity, which you probably don't want to.
Answer by Thelo · Nov 16, 2010 at 10:38 AM
Most platform games have a maximum speed at which you fall down (or jump up), I'll assume that this is what you mean. In that case, just limit the maximum vertical speed difference you apply to the character:
var V_SPEED_LIMIT : float = 5.0;
var limitedVerticalSpeed : Vector3 = Vector3( moveDirection.x, Mathf.Clamp(moveDirection.y, -V_SPEED_LIMIT, V_SPEED_LIMIT), moveDirection.z);
charController.Move(limitedVerticalSpeed * Time.deltaTime);
As spree pointed out, you probably don't want to apply your walkSpeed to gravity.
But that applies to the general movement, not the falling part. If I alter that then the entire movement system is affected.
Your answer
Follow this Question
Related Questions
Problem of gravity 1 Answer
unity 5 game 2.5 trouble jumping 2 Answers
Constant speed when free-falling... Why?? 1 Answer
Rigidbody y velocity is stuck on 0, gravity is not turned off. 2 Answers