Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 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 daragus_innovations · Nov 12, 2017 at 11:34 AM · androidplayerrigidbody2dvelocitylistener

rigidbody velocity not working

Hello,

I currently have a small Problem with my game. And I really run out of ideas. I traced the problem to the velocity function of a rigid body. Heres my code

 Rigidbody2d  player = GetComponent<Rigidbody2D>();
 
 Update loop:
 
 if (Input.GetMouseButton(0))
             {
               player.velocity = vector2.zero;
 }
 
 if (Input.GetMouseButtonUp(0)){
               player.velocity = vector2.zero;
 }

The GetMouseButton Function always works. But the GetMouseButtonUp only works sometimes. Both listeners are called just fine. I tested it by writing to the log if a listener was called. But the velocity doesnt always seems to change. Has anyone any idea?

Kind regards, Colin

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by WbrJr · Dec 05, 2017 at 08:48 PM

Hello there, I know the answer is a bit late, but it propably will help future searchers. :)

I think you have to pultiply the vector with a speed value. Like this:

 public float speed = 10f;
 Rigidbody2d  rb2d;
 
 void Start()
 {
 rb2d = GetComponent<RigidBody2D>();
 }
  
  void Update ()
  {
  if (Input.GetMouseButton(0))
              {
                player.velocity = vector2.right * speed;
  }
  
  if (Input.GetMouseButtonUp(0)){
                player.velocity = vector2.left * speed;
  }
 }


I didnt try it, but it should do something you can work with.

have a nice day,

WbrJr

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

Answer by Bekatam · May 17, 2020 at 11:18 AM

Use this format instead:

        public float speed = 10f;
        RigidBody2D rb;
        void Start()
        {
               rb = GetComponent<RigidBody2D>();
        }
        void Update
        {
              if(Input.GetMouseButtonDown(0))
              {
                    rb.velocity = new Vector3((speedx * speed), speedy, speedz); 
              }
         }


  • N.B: speedx, speedy and speedz are my own arbitrary values, you can use whatever variable values you want. This code can be improved but I'm pretty sure this will solve most of your problems. I hope I helped whoever was struggling with this(I too struggled with this before so I know your pain).

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

Answer by ziyadedris · Jun 17, 2021 at 11:15 AM

very weird glitch but here is how to fix it
just multiply by 0
rb.velocity = Vector3.zero * 0;
it doesn't always work, but it makes the glitch A LOT more rare so yeah, hope it helped

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

162 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

Related Questions

Player parent to platform problem 0 Answers

Android velocity slower than editor 0 Answers

My 2D character is hovering away from the ground and walls for some reason, and is also experiencing some other issues,My 2D Player hovers over the Ground and Wall Tiles I have set up, don't know why 0 Answers

Unity background sound and player controll ? 0 Answers

rigidbody2D.velocity.y always returns 0 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