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
0
Question by SuperBiasedMan · Apr 12, 2013 at 03:05 PM · 2dplatformer

Box not jumping, just jitters like it's stuck to the ground (2D platformer movement)

Sorry if this has been asked before but I couldn't find exactly what I wanted. I'm trying to set up an object that will jump when the jump button is pressed. I want the button to instantly set upward velocity to the y vector but that I then slowly depreciate until natural gravity is a greater force and pulls the character back down to a surface to land.

Unfortunately what I'm getting is that the box I'm using to test doesn't move up, it just jitters on the surface and then seemingly at random it leaps up huge distances, like it has just overcome the earth's gravitational pull.

I'd be very much obliged if someone could explain where my code is flawed and leads to this. I know there are probably tutorials that could show me exactly how to do this but I'd like if I could understand what about this approach was failing.

Here's my code:

 #pragma strict
 var speed: int;
 private var distY: float = 0;
 var artificialGravity: float;
 var midair = false;
 var jumping: float;
 private var jumpingHeight: float;
 
 function Update () {
     if(Input.GetButtonDown("Jump")){
         jumpingHeight = jumping;
         midair = true;
     }
 
     var distX = Input.GetAxis("Horizontal")*Time.deltaTime*speed;
     var distY = jumpingHeight*Time.deltaTime;
     
     jumpingHeight*=artificialGravity;
             
     transform.Translate(distX,distY,0);
 }
 
 function OnCollisionEnter(collision: Collision){
     if (!Input.GetButton("Jump")){
         midair = false;
         jumpingHeight = 0;
     }
 }

Thanks in advance for anyone who can help. :)

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 whebert · Apr 12, 2013 at 03:09 PM 0
Share

Is a RigidBody attached to this object?

1 Reply

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

Answer by whebert · Apr 12, 2013 at 03:20 PM

If your object is a RigidBody, you could just let Unity Physics do the work...

 void Update () {
     
         if(Input.GetButtonDown("Jump"))
         {
             if(!midair)
             {
                 rigidbody.AddForce(Vector3.up * 500);
                 midair = true;
             }
         }
     }

EDIT: Forgot you were using Javascript

 function Update () {
 
     if(Input.GetButtonDown("Jump"))
     {
         if(!midair)
         {
             rigidbody.AddForce(Vector3.up * 500);
             midair = true;
         }
     }   
 }

EDIT: Forgot to just use convenience property rigidbody.

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 whebert · Apr 12, 2013 at 03:25 PM 0
Share

And of course, the amount of force you apply is dependent on the mass of your RigidBody (if you're using that). Since you're using OnCollisionEnter, I assume you are.

avatar image SuperBiasedMan · Apr 15, 2013 at 11:49 PM 0
Share

That worked great, thanks. :D

I wasn't aware of this function before, I guess I have some reading up to do on vector operations. :P

(Also sorry I was slow to respond, I was away for a couple of days, didn't think I'd get an answer so fast)

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

11 People are following this question.

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

Related Questions

Why is my gameObject falling so slow? 1 Answer

GUI DrawTexture is sliding across the screen? 0 Answers

2d collider stuck issue 1 Answer

Hinge joint 2D moving away from anchor 0 Answers

animation 2d platformer 2 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