- Home /
Need help with making a character jump.
So, I've started on my own character movement script and can't seem to make my character jump without continuously flying if I continue to hold space down. I've looked around online for a solution and found a few jumping tutorials but they're not making very realistic jumps, can someone help me fix this? Here's my code so far (I've removed all the script I had for jumping)
var rotationSpeed: float = 10;
var walkSpeed : float = 3;
var gravity : float = 50;
private var yRot : float;
function Update ()
{
var Controller : CharacterController = GetComponent(CharacterController);
var vertical : Vector3 = transform.TransformDirection(Vector3.forward);
var horizontal : Vector3 = transform.TransformDirection(Vector3.right);
var height : Vector3 = transform.TransformDirection(Vector3.up);
if(Input.GetAxis("Vertical")||Input.GetAxis("Horizontal"))
{
animation.CrossFade("Default Walk",0.2);
Controller.Move((vertical * (walkSpeed * Input.GetAxis("Vertical"))) * Time.deltaTime);
Controller.Move((horizontal * (walkSpeed * Input.GetAxis("Horizontal"))) * Time.deltaTime);
}
else
{
animation.CrossFade("Default Idle",0.2);
}
if(Input.GetAxis("Mouse X"))
{
yRot += 10* Input.GetAxis("Mouse X");
}
transform.rotation = Quaternion.Euler(0,yRot,0);
Controller.Move(height * gravity * Time.deltaTime);
}
You should take a look at the example script at CharacterController.$$anonymous$$ove(). It implements gravity. If this is what is done in the tutorials, then what "realism" issues do you have with the code?
yeah the tutorials were implementing gravity. The "realism" issues were when I would jump I would almost instantly appear 4 times the height of my character in the air, and then would descend very VERY slowly. There was also the ability for me to "Fly" if I continued to hold down the Space button. But thank you for the link, I'll check that out.
You can start by using the example script and a character controller on an object and see if the behavior is acceptable.
Answer by Recluse · Oct 09, 2013 at 05:33 AM
To prevent your character flying check for contact with the ground before calling the jump function...
if (controller.collisionFlags & CollisionFlags.Below) {
// do jump stuff here
}
after tweaking the gravity around I managed to get the jumping working pretty good, thanks for your tip on not flying.