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 FrowningPigeon · Jan 18, 2014 at 01:46 PM · 2dphysicsrigidbody2djump

Rigidbody2d not falling once in the air

Hi all,

I am currently learning how to create a 2d game using the new 2d feaures that came with 4.3. I have a script which allows the character to move left and right, and also to jump (well kind of). The problem is, is that once the character is in mid-air; it does not fall....instead just stays at whatever position it got to.

Here is the script I am using:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour
 {
     private Transform groundCheck;            // A position marking where to check if the player is grounded.
     private Animator animator;
     private bool grounded = false;            // Whether or not the player is grounded.
     public bool jump = false;
     float vertSpeed = 0.0f;
     float gravity = 20.0f;
     private int jumpHeight = 500;
     
     // Use this for initialization
     void Start()
     {
         groundCheck = transform.Find("groundCheck");
         animator = this.GetComponent<Animator>();
     }
 
     // Update is called once per frame
     void Update()
     {
         // The player is grounded if a linecast to the groundcheck position hits anything on the ground layer.
         grounded = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
         //if (Input.GetAxis("Vertical") > 0.0f && grounded)
             //jump = true;
         if(Input.GetButtonDown("Jump") && grounded)
             jump = true;
         
     }
 
     void FixedUpdate()
     {
         var vertical = Input.GetAxis("Vertical");
         var horizontal = Input.GetAxis("Horizontal");
         float h = Input.GetAxis("Horizontal");
         
         if (Input.GetAxis("Horizontal") > 0.0f)
         {
             animator.SetInteger("Direction", 0);
             rigidbody2D.AddForce(Vector2.right * h * 120);
             //rigidbody2D.AddForce(transform.forward * 100);
         }
         
         if (Input.GetAxis("Horizontal") < 0.0f)
         {
             animator.SetInteger("Direction", 1);
             rigidbody2D.AddForce(Vector2.right * -120);
         }
         if (Input.GetAxis ("Vertical") > 0.0f)
         {
             rigidbody2D.AddForce(Vector2.up * vertical * 500);
 
         }
 
     }
 
 }

And here is a screenshot of how my character is set up:

alt text

Thanks,

Callum

In addition, when I set the gravity to higher than 0; my character just floats up perpetually forever?

gamescreen1.jpg (328.1 kB)
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
0

Answer by KellyThomas · Jan 18, 2014 at 01:48 PM

Use the inspector to set gravity scale to a non-zero value.

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 FrowningPigeon · Jan 18, 2014 at 01:49 PM 0
Share

I have tried this, but what happens is my character just floats up ; it does not stop then fall?

avatar image KellyThomas · Jan 18, 2014 at 01:57 PM 0
Share

Please see the docs, it should be used as a multiplier to the gravity defined in the Physics 2D $$anonymous$$anager.

avatar image FrowningPigeon · Jan 18, 2014 at 02:01 PM 0
Share

I have sorted it now, I needed to check the 'Animate Physics' on the animator, and also check 'Fixed Angle' on the rigodbody2d. Thanks for you help!

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

19 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can I move a 2D GameObject left/right relative to its forward direction? 1 Answer

2D objects gets stuck on the ground/wall? 12 Answers

Convert Force into Velocity for 2D Player Jump 0 Answers

My player jumps higher when the ground is aslant? 1 Answer

Paper Ball Physics Implementation 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