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 S_Byrnes · Mar 18, 2016 at 07:22 AM · c#2dmousevelocityplatform

Setting Velocity of Rigidbody to Mouse Speed

Hello all,

I'm having some trouble working out what's wrong with my code. To me it looks fine but obviously that's not the case or I wouldn't be having problems.

Basically I have a 2D sprite that I need to move with the mouse's x position, I need it to move physics objects so I'm setting it's rigidbody.velocity. What I need is to have the speed of the sprite match the speed of the users mouse.

Right now it half works, it will follow the mouse if it's on the right side of it, but very slowly (so I tested multiplying the value I have by a float called 'speed', but I've removed that for the code to show how it looked before).

Anyway, the issue is that on the right side, it will move left and right correctly. If on the left side, it will move in opposite directions to the mouse. It's hard to explain so hopefully you can see the problem in my code.

         float distance_to_screen = Camera.main.WorldToScreenPoint(gameObject.transform.position).z;
 
         Vector3 pos_move = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, distance_to_screen));
 
         mouseDelta = pos_move - lastMousePosition;
 
         //if (Mathf.Abs(transform.position.x - pos_move.x) >= 1)
         //{
 
         if (platform.position.x < pos_move.x)
             {
                 platform.velocity = Vector2.right * mouseDelta.x;
             }
 
             else //(platform.position.x > pos_move.x)
             {
                 platform.velocity = Vector2.left * mouseDelta.x;
             }
 
         lastMousePosition = pos_move;
 
     }

All help is highly appreciated thank you!

Comment
Add comment · Show 1
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 S_Byrnes · Mar 20, 2016 at 12:41 AM 0
Share

Alright so I've got it to where it's on the correct side, but it doesn't follow the mouse directly, it will only take it's speed in that direction and keep moving unless I change the mouse position.

What I was trying to do was have the platform ease into the mouse's location (both acceleration and deceleration) until it's exactly where the mouse is.

Here is what I have now:

 acceleration = mouseDelta.x * 0.4f;
         platformVelocity += acceleration;
         platform.velocity = new Vector2(platformVelocity, 0) * speed;

That code is in-place of the "if (platform)" statements.

Any ideas? I'm quite stuck.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Rob2309 · Mar 23, 2016 at 01:37 PM

As far as I can see I would try dividing your mouseDelta by Time.deltaTime, as this should technically give you the real velocity then...

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

128 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

Related Questions

Is there a way to lock velocity? 3 Answers

Unity C# 2D Adding Velocity on rotation 1 Answer

Velocity powered rigidbody on a moving platform without parenting. 3 Answers

If i click on a object i move to the next level (c#) 0 Answers

Create objects on a mouse click in 2D 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