- Home /
How do I fix my player jumping code?
Help everyone, I have the following variable in PlayerMovement.js, the script that I use for movement:
var jumpSpeed = 1.0;
I also have the following Update() function:
function Update ()
{
var controller : CharacterController = GetComponent(CharacterController);
// Rotate around y - axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);
// Move forward / backward
var forward = transform.TransformDirection(Vector3.forward);
var curSpeed = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * curSpeed);
// Jump code
if (Input.GetButtonDown("Jump"))
{
var upward = transform.TransformDirection(Vector3.up);
controller.Move(upward * jumpSpeed);
}
// Animate character
if (Input.GetAxis("Vertical") > 0.1 || Input.GetAxis("Horizontal") > 0.1 || Input.GetAxis("Vertical") < -0.1 || Input.GetAxis("Horizontal") < -0.1)
animation.CrossFade("run");
else
{
animation.CrossFade("idle");
}
}
The character's movements and animations work just fine, but currently, the jump code merely drops the player character from a height equal to jumpSpeed above the player's position in 3d space. The character must obviously have a smooth vertical transformation before dropping. Please help, it will be appreciated.
MachCUBED
Edit your post and get your code formatted correctly, then you'll be more likely to get someone to help with it. The code block on this site is a pain to get to work right at times, but it's also a pain trying to read ugly script. --Not being a jerk, just saying that since you're the one asking for help then make it easier for those that would help you.
Answer by aldonaletto · Dec 18, 2011 at 01:41 AM
SimpleMove takes care of the vertical movement to include gravity, what is causing this weird behavior. You should use only Move to be able to jump, and apply gravity yourself.
Unlike SimpleMove, to which you pass the velocity, Move requires a displacement. Fortunately, you just need to multiply the velocity by Time.deltaTime to convert it to a displacement:
var jumpSpeed: float = 5.0; var speed: float = 6.0; var rotateSpeed: float = 60.0; var gravity: float = 10.0; // gravity acceleration
private var vSpeed: float = 0; // store vertical speed in a separate variable
function Update () { var controller : CharacterController = GetComponent(CharacterController); // Rotate around y - axis transform.Rotate(0, Input.GetAxis ("Horizontal") rotateSpeed Time.deltaTime, 0); // Move forward / backward var curSpeed = speed Input.GetAxis ("Vertical"); var moveDir = transform.forward curSpeed; // Jump code if (controller.isGrounded){ // if character is grounded... vSpeed = 0; // its vert speed is zero if (Input.GetButtonDown("Jump")) { // but if jump pressed, set it to jumpSpeed vSpeed = jumpSpeed; } } // apply gravity acceleration vSpeed -= gravity Time.deltaTime; moveDir.y = vSpeed; // include vSpeed in moveDir // moveDir Time.deltaTime is the displacement since last frame controller.Move(moveDir * Time.deltaTime); } NOTE: remember to set jumpSpeed to a more suitable value - 5 to 10 - at the Inspector; the Editor has an elephant memory, and will keep the old value (1.0) to death, which will make the jump a mere hiccup instead of a decent jump!
hey aldonaletto, i'm just curious because i've been working on the same problem as well the last couple of $$anonymous$$utes. and i actually wrote quite the same code as you did. i just didn't add a Time.deltaTime to the rotation, i mean i understand it, i'm just curious if it's necessary or just nice to have it framerate independent and to know exactly how many degrees the object will rotate per second.
cheers, fox
Congratulations aldonaletto! Your script fixed everything, even though its slightly different from the simple movement script that I had previously been using only for directional movement (I had to try to bolt some jumping code onto it earlier).
Answer by Spitaris · Dec 17, 2011 at 11:30 PM
Hello,
Is your character a Rigidbody ? If it is, you should try to change the velocity variable on it, making it somthing like a Vector3(0, jumpSpeed, 0);
Hoping I helped you
I changed my jump code to look like this:
if (Input.GetButtonDown("Jump"))
{
var upward = transform.TransformDirection(Vector3.up);
controller.Velocity = (controller.Velocity + Vector3(0, jumpSpeed, 0));
}
It now prints the following console error if I try to jump:
NullReferenceException: Object reference not set to an instance of an object
Boo.Lang.Runtime.RuntimeServices.InvokeBinaryOperator (System.String operatorName, System.Object lhs, System.Object rhs)
Player$$anonymous$$ovement.Update () (at Assets/Player/Scripts/Player$$anonymous$$ovement.js:21)
Even if I change the velocity.y increase code to this:
controller.Velocity.y += jumpSpeed;
It still spits out the same error. Am I doing my vector code wrong?
Having fixed the letter-case issue, I've just noticed that velocity in CharacterController is read-only.
Ok, i fixed it ages ago and forgot to post it. Here is the solution:
// $$anonymous$$ove forward / backward // $$anonymous$$ove forward / backward var curSpeed = speed * Input.GetAxis ("Vertical"); var moveDir = transform.forward * curSpeed;
// Jump code
if (controller.isGrounded)
{
// if character is grounded...
vSpeed = 0; // its vert speed is zero
if (Input.GetButtonDown("Jump"))
{ // but if jump pressed, set it to jumpSpeed
vSpeed = jumpSpeed;
}
}
// apply gravity acceleration
vSpeed -= gravity * Time.deltaTime;
moveDir.y = vSpeed; // include vSpeed in moveDir
// moveDir * Time.deltaTime is the displacement since last frame
controller.$$anonymous$$ove(moveDir * Time.deltaTime);
Thanks for the help guys,
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Translation of an object 2 Answers
[C#] Jump on slopes 1 Answer