• 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 Christian.Tucker · Feb 12, 2014 at 08:21 PM · c#collisionphysicscharacter

Two Basic Physic Questions (Gravity and Jumping)

So.. I've never really had this problem with the 3D games I've made, because gravity was supposed to make you fall to the ground.. and ultimately... stay there, however for the game I'm working on now that's not the case.

The goal is actually to... NOT hit the bottom. Think of concepts such as the infamously hated Flappy Bird, or Jetpack Joyride. Where you are constantly falling down, but have to stay up.

Here's my problem.

Gravity: It seems like gravity constantly increases. Like it's a multiplier. The longer you fall, the faster you fall. This is unacceptable for my game style. I need to fall down at a constant rate.. While... I'm not "Jumping" or.. "Propelling" or.. "Flying upwards" or whatever.

Jumping: I tried using the following code.. but nothing happens when I jump. Literally. I mean any debug calls I make in the method go off, but the character does not raise, or do anything besides continue to fall to his immediate death.

 rigidbody.AddForce(Vector3.up * forceApplied);


First 2D game, sorry if the questions are a little strange, I've been working with 3D games where gravity does not apply, recently.


Really confused on what I'm doing wrong here.

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 1elfdragon1 · Feb 12, 2014 at 10:39 PM

 using UnityEngine;
 using System.Collections;
 
 public class characterController : MonoBehaviour
 {
     public float jumpPower = 2.0f;
     public float gravity = 9.81f;
     public float velocityChange = 2.0f;
     private float forceApplied = 0.0f;
     private Rigidbody2D r = null;
 
     void Start()
     {
         r = GetComponent<Rigidbody2D>();
     }
 
     void FixedUpdate()
     {
         if (Input.GetButtonDown("Jump"))
         {
             r.velocity = new Vector2(r.velocity.x, Mathf.Sqrt(2 * jumpPower * gravity));
         }
         float velocity = Mathf.Max((gravity - r.velocity.y), 0.0f);
         forceApplied = (gravity - velocity);
         forceApplied = Mathf.Clamp(forceApplied, -gravity, gravity);
         r.AddForce(new Vector2(0.0f, -forceApplied));
     }
 }

first i checked what the difference between gravity and the velocity (also checked if the velocity is already the gravity). then put a maximun velocityChange to the force.

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

OnCollisionStay only detects collisions after a secondary collision 0 Answers

Handling colliders on hundreds of asteroids (not procedural asteroids) ...URGENT 3 Answers

Performance Question on Kinematic Rigidbodies 1 Answer

How can I play audio while a non player controlled object is moving and stops playing when object stands still? 1 Answer

Displaying text on trigger enter 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