Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 gowatcherr · May 26, 2018 at 05:02 PM · unity 2dvelocityvector2basic programming

how come object is moving when i pressed nothing?

Please some one explain me how my object is moving when i set movement to 0 and if statement clearly states that rigidbody2d shouldn't move when movement = 0. but when i run program ball move due to gravity even with movement is zero.. please someone explain this i have spent 2 hour on this problem and now my brain is staring to hurt

 public class Controller : MonoBehaviour {
         public float movement=0;
         public float speed = 6;
         private Rigidbody2D RD;
         // Use this for initialization
         void Start () {
              RD = GetComponent<Rigidbody2D>();
             
         }
         
         // Update is called once per frame
         void Update ()
         {
             
     
             if(movement>0)
             { RD.velocity = new Vector2(movement*speed,RD.velocity.y);  }
             else if(movement<0)
             { RD.velocity = new Vector2(movement * speed, RD.velocity.y); }
     
         }
     }


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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by _samuel12345 · May 26, 2018 at 05:42 PM

You are only changing the velocity if movement is less than or greater than 0 - not equal to it. Try:

 else if(movement == 0){
 RD.velocity = Vector2.zero;
 }

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 gowatcherr · May 26, 2018 at 06:15 PM 0
Share

i am not asking for solution i am asking "why object is moving when the required condition is not satisfied"?

avatar image MT369MT · May 26, 2018 at 06:31 PM 1
Share

Like @_samuel12345 explained, you have never said that if the movement is 0 the object must NOT move. So if gravity is on the object will go downwards.

avatar image gowatcherr MT369MT · May 26, 2018 at 08:46 PM 0
Share

so object will move regardless of object.velocity because far as i know velocity make object move. so having movement==0 means object should move automatically. Is this some built in functionality into unity?

avatar image
0

Answer by Tobychappell · May 26, 2018 at 09:37 PM

Just tested your code and yes it doesn't set the velocity when movement = 0.


But if movement was equal to 10 and you hit play, the code will constantly set the velocity to (10,0).


The rigidbody will then move in the direction (10,0).


Whilst still in play mode.. you then set movement to 0....


The rigidbody will still move as its velocity is still (10,0).


The reason for this is because nothing else is acting on it, your code is't telling it to stop moving when movement = 0. Think of it drifting in space, if you push on something it will remain travelling at the speed that it was at when it left contact from your hand. Im assuming your linear drag is 0 (which is default when you create a Rigidbody2D) so the physics engine wont even be slowing it down.

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.

Follow this Question

Answers Answers and Comments

86 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

Related Questions

Rigidbody2D.Velocity = Vector2 not changing X Position 1 Answer

Velocity with (unit vector) * (value) problem 1 Answer

calculate velocity vector based on direction 1 Answer

Increasing the speed of a ball in a brick breaker game 0 Answers

2 sprites. Same Script. Different Velocities. 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