Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Ace2655 · Mar 05, 2019 at 08:41 PM · physicsgravityformula

Why does using the velocity of y for the y velocity value work to simulate gravity?

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerController : MonoBehaviour {

 public Rigidbody rb;

 public float forwardForce = 1000;
 public float sidewaysForce = 50;
 public float downwardForce = 20;

 int movementX;

   

 // Start is called before the first frame update
 void Start()
 {
     movementX = 0;
 }

 void FixedUpdate()
 {
     if (transform.position.y > 1.1)
     {
         rb.AddForce(0, -downwardForce, 0);
     }
     

     if (transform.position.y < 0)
     {
         FindObjectOfType<GameManager>().EndGame();
     }

     rb.velocity = new Vector2(movementX, rb.velocity.y);
 }

 // Update is called once per frame
 void Update()
 {
     rb.AddForce(0, 0, forwardForce * Time.deltaTime * 100);


     
     if (Input.GetKeyDown("right") || Input.GetKeyDown("d"))
     {
         //rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
         movementX = 15;
     }

     if (Input.GetKeyDown("left") || Input.GetKeyDown("a"))
     {
         //rb.AddForce(-1 * sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
         movementX = -15;
     }

     if (Input.GetKeyUp("right") || Input.GetKeyUp("d"))
     {
         movementX = 0;
     }

     if (Input.GetKeyUp("left") || Input.GetKeyUp("a"))
     {
         movementX = 0;
     }

 }

}

Basically I have this code here and it works but I'm not exactly sure why. I was hoping someone could explain to me why rb.velocity = new Vector2(movementX, rb.velocity.y); simulates gravity and if I was to replace to "rb.velocity.y" with zero than my object floats.

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
Best Answer

Answer by Kishotta · Mar 05, 2019 at 08:51 PM

This is kind of an artifact of the Unity API for Vector2 (and 3 and 4). You can't just do myVector.x = 5 to alter a vector because the way the Vector structs are designed, the .x, .y, and .z properties return a copy of their values. So you'd be setting some value equal to another value, but not actually changing your vector.

The physics system automatically updates the Velocity values of all rigidbodies, so when you create your new Vector with rb.velocity = new Vector2 (movementX, rb.velocity.y); you are overwriting the velocity by constructing a new Vector that has a new horizontal value, but preserves the old vertical value that was supplied to the rigidbody by the physics system (gravity in your case).

When you don't pass in the old (physics supplied) vertical component to the new movement vector, the gravity isn't preserved and your object appears to float. You're essentially just throwing away the physics system's gravity numbers.

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 Ace2655 · Mar 06, 2019 at 01:41 AM 0
Share

Thank you for the info!

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

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

Related Questions

Affecting FPS controller/Character Motor physics with game object 1 Answer

I am getting LOW Fps, because of simple Physics. Help. 5 Answers

Efficient faux gravity script 1 Answer

How can I make a physics base projectile move along the path of a sphere? 1 Answer

Airplane Stall 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