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 Spite · Jun 05, 2015 at 06:14 AM · 2d gamejump2d physics

simple jump-script (2D)

I just started to make my first game with Unity - a simple Jump 'n Run. I used Rigidbody2D.AddForce for the jump mechanic. Right now I can jump out of the window, if I hold the UpArrow down. What I am looking for is an option to maximize the jump height. I tried to build a trigger-box and putting the jumping-script into an OnTriggerStay-function, but this doesn't work. As I can't find an answer in the Manual, Scripting API or the internet I would appreciate some help :)

That's my current code:

 public class Player : MonoBehaviour 
 {
     public float moveSpeed;
     public float jumpPower;
 
     void Update () 
     {
         if (Input.GetKey(KeyCode.RightArrow))
             transform.Translate(Vector3.right * moveSpeed * Time.deltaTime);
         if (Input.GetKey(KeyCode.LeftArrow))
             transform.Translate(-Vector2.right * moveSpeed * Time.deltaTime);
 
         Rigidbody2D rb = GetComponent<Rigidbody2D> ();
         if (Input.GetKey(KeyCode.UpArrow))
             rb.AddForce(Vector2.up * jumpPower);
     }
 }

What I found in the Scripting API was AddForce.impulse. This sounds like what I am looking for. But unfortunately I am not able to implement it.

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 Andres-Fernandez · Jun 05, 2015 at 06:55 AM 0
Share

Not an answer to your question but just a quick tip. If you use a rigidbody2D, then don't use transform.Translate to move the object, use RigidBody2D.$$anonymous$$ovePosition ins$$anonymous$$d, since transform.Translate ignores physics but rigidbody2d.$$anonymous$$ovePosition produces collisions and triggers.

1 Reply

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

Answer by Andres-Fernandez · Jun 05, 2015 at 06:59 AM

This tutorial has great tips on making a 2D character controller you can apply. I suggest you use a grounded flag approach (using a boolean to check if the object is touching the ground and not allowing it to jump if it's not grounded).

Comment
Add comment · Show 1 · 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 Andres-Fernandez · Jun 05, 2015 at 07:04 AM 0
Share

Also check the Input.Get$$anonymous$$eyDown function to make the jump only once when you press the key.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

[Solved] Having trouble with 2D Character Jump using Bolt 0 Answers

How can I change jump direction? 1 Answer

Problem with Jump 1 Answer

Rigidbody2D.MovePosition is not being instaneous 0 Answers

Jump Function C# hassle! 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