Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by MachCUBED · Dec 17, 2011 at 05:30 PM · movementcharactercharactercontrollerjump

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

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Lo0NuhtiK · Dec 17, 2011 at 10:51 PM 2
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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!

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image mrFox · Dec 18, 2011 at 02:07 AM 0
Share

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

avatar image MachCUBED · Dec 18, 2011 at 05:14 AM 0
Share

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).

avatar image
0

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

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MachCUBED · Dec 17, 2011 at 11:52 PM 0
Share

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?

avatar image mrFox · Dec 18, 2011 at 12:22 AM 0
Share

might be that it should be "velocity" not "Velocity"

avatar image ThomasQ · Dec 18, 2011 at 12:33 AM 0
Share

yeah, no capital V..

avatar image MachCUBED · Dec 18, 2011 at 12:41 AM 0
Share

Having fixed the letter-case issue, I've just noticed that velocity in CharacterController is read-only.

avatar image MachCUBED · Dec 30, 2011 at 06:59 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Character Controller sticky head.... Help!? 1 Answer

CharacterController unexpectedly moving up 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges