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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by galaboy · Jan 06, 2013 at 07:13 AM · scriptingproblem

problem with the jump code..

iam learning eteeski's fps1.5 jumping the right way game tutorial in which, my player has to jump when i press space bar.the code for jumping is as follows,


     var walkacceleration : float;
     var cameraobject : GameObject;
     var maxwalkspeed : float = 20;
     @HideInInspector
     var horizontalmovement : Vector2;
     var jumpvelocity : float = 20;
     @HideInInspector
     var grounded : boolean = false;
     var maxslope : float = 60;
     function Start () {
     }
     function Update () {
     horizontalmovement = Vector2(rigidbody.velocity.x, rigidbody.velocity.z);
     if(horizontalmovement.magnitude > maxwalkspeed )
     {
     horizontalmovement = horizontalmovement.normalized;
     horizontalmovement *= maxwalkspeed;
     }
     rigidbody.velocity.x = horizontalmovement.x;
     rigidbody.velocity.z = horizontalmovement.y;
     transform.rotation = Quaternion.Euler(0 ,cameraobject.GetComponent(lookupscript).currentyrotation, 0);
     rigidbody.AddRelativeForce(Input.GetAxis("Horizontal") * walkacceleration, 0 , Input.GetAxis("Vertical") * walkacceleration);
 
     **if(Input.GetButtonDown("Jump"))
     rigidbody.AddForce(0,jumpvelocity,0);**
     }


the problem is my player is not jumping when i press my spacebar. can any one help me.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by AlucardJay · Jan 06, 2013 at 07:49 AM

Force is applied over time, but your jump code only applies the force when the spaceBar is pressed. To use force like this (triggered once but affect the rigidbody properly), you need to use ForceMode.Impulse

eg :

 if(Input.GetButtonDown("Jump")) {
     rigidbody.AddForce( Vector3(0,jumpvelocity,0), ForceMode.Impulse );
 }

http://docs.unity3d.com/Documentation/ScriptReference/ForceMode.Impulse.html

http://docs.unity3d.com/Documentation/ScriptReference/ForceMode.html

http://docs.unity3d.com/Documentation/ScriptReference/Rigidbody.AddForce.html

Comment
Add comment · Show 3 · 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 galaboy · Jan 06, 2013 at 08:20 AM 0
Share

thank u jay kay for ur valuable help, its working now. but in the tutorial wat iam studyin this is the code the tutor used and it worked.

avatar image AlucardJay · Jan 06, 2013 at 08:22 AM 0
Share

not sure then, but glad it worked. Can you link the tutorial? I shall have a look.

avatar image AlucardJay · Jan 06, 2013 at 11:29 AM 0
Share

gagaboy : here is the link for the tutorial : http://www.youtube.com/watch?v=eSnzuRgrN_8&list=PL7AE076AFAFD3C305

$$anonymous$$e : it looks like you didn't finish the video. Go to 21:30, there is more code you havn't got in your question, eg

 if ( Input.GetButtonDown("Jump") && grounded )

and then the collision function, etc etc. I suggest finishing the course, as changing the code will have strange results.

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

9 People are following this question.

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

Related Questions

Unity Editor Crashes On Play Because of Import Script 1 Answer

Script Error with CharacterMotor 1 Answer

How can I reduce speed of an object, the more that object gets close to target? 1 Answer

How to use a controller button and keyboard key? 0 Answers

Making a character jump and crouch using a Joystick 1 Answer


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