Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 GregoryNeal · May 17, 2015 at 05:01 AM · movementvector3rigidbody2d

Simple movement script improper behaviour

Hi I'm trying to move a simple sprite in the direction of my mouse at a constant velocity. Here is what I have

     void Update()
     {
         mousePos = Input.mousePosition;
         mousePos = Camera.main.ScreenToWorldPoint(mousePos);
         mousePos.z = zPos;
         mousePos = mousePos.normalized;
         rb.velocity = mousePos * maxSpeed;
 
         Debug.DrawLine(transform.position,mousePos);
 
     }

Where zPos is defined in Start() as the z position of my player sprite. Whenever I run this code the sprite just moves in the same horizontal direction with ever increasing speed but I can alter the vertical movement. What am I 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 Eno-Khaon · May 17, 2015 at 05:16 AM

Your movement isn't relative. Let's say your mouse position in world space is Vector3(1000, 20, -800). Your character is standing at Vector3(998, 20, -800). You're not going to move right. You're going to move in the direction of Vector3(1000, 20, -800).normalized.

What you need to do is get the relative direction:

 mousePos = Input.mousePosition;
 mousePos = Camera.main.ScreenToWorldPoint(mousePos);
 mousePos.z = zPos;
 mousePos = (mousePos - transform.position).normalized;
 rb.velocity = mousePos * maxSpeed;

Now, on the other side of things, you really, REALLY shouldn't set your rigidbody velocity directly, but that's for other reasons generally found every other day here.

In short, the better solution to setting velocity is to use AddForce().

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 GregoryNeal · May 17, 2015 at 05:24 AM 0
Share

$$anonymous$$ay I ask why setting the velocity is improper? I like the snappy movement feel that setting the velocity gives. But I'm willing to replace it if it's going to cause issues later.

avatar image Eno-Khaon · May 17, 2015 at 05:34 AM 0
Share

Well, I suppose it's a bit of an exaggeration to say it quite like that. Under most circumstances, if you're relying on any automatically physics-driven behavior, you can experience awkward, "unrealistic simulation" if you do.

Just as an example, if you're setting a character's velocity directly in a situation where they're bumping against something, the other object won't be able to reliably force back the character whose velocity is always being reset every frame of motion. Rather than bouncing off of the other object, the object is simply being forced aside with greater and greater force every frame.

So, really, I suppose it's mainly a matter of context, in that it's not the best choice when there are other physics-driven objects to interact with.

avatar image GregoryNeal · May 17, 2015 at 05:46 AM 0
Share

That makes sense and it looks like AddForce is what I want anyways thanks.

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

20 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

Related Questions

Convert WASD to local rotation 1 Answer

Limit object position which is moved by accelerometer 0 Answers

MoveTowards inside Coroutine 2 Answers

Smooth motion physics Playmaker 0 Answers

Character won't move (Fixed) 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