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 Skeebo87 · Mar 21, 2021 at 06:30 AM · vector3rigidbody2dvelocityvector2

Rigbody 2D Velocity Mystery

I am fairly new to rigidbody velocity and how to get it to work properly. Currently, with the code below, my player marker will move across the screen towards where I last clicked on the screen. Once it arrives at where I last clicked it will stop. All of this works just as I had hoped. My only issue is that when I click somewhere relatively far (player marker on left side of screen and I click right side of screen) my marker moves at a desirable speed, but if I click relatively close to where my marker currently sits then I notice the velocity seems extremely low. Can anyone help me fix my code to where my marker will always move at a set velocity to its destination, no matter how far or near? Thanks!

 public class PlayerMarker : MonoBehaviour
 {
     Rigidbody2D rig;
     public float moveSpeed;
     private Vector3 targetDestination;
     void Start()
     {
         rig = GetComponent<Rigidbody2D>();
     }
 
     void Update()
     {
         if(Input.GetKeyDown(KeyCode.Mouse0))
         {
             targetDestination = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             Move(targetDestination);
         }
 
         float distanceFromDestination = Vector2.Distance(transform.position, targetDestination);
         if(distanceFromDestination <= .01)
         {
             rig.velocity = Vector2.zero;
         }
     }
 
     void Move(Vector3 destination)
     {
         Vector2 dir = (destination - transform.position).normalized;
         rig.velocity = dir * moveSpeed;
     }
 }
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 Skeebo87 · Mar 21, 2021 at 07:48 AM

I use a similar logic for enemy markers that will chase me and their movement speed across the screen does not change based on distance from their target (my player marker). The only thing these two scrips do not have in common is the use of ScreenToWorldPoint. I'm not sure if my problem lies somewhere in regards to how I use ScreenToWorldPoint to calculate my target destination for my player marker.

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 Skeebo87 · Mar 21, 2021 at 07:58 AM 0
Share

I have discovered the issue. At the top of my code I have set targetDestination to be a Vector3 instead of a Vector2. I believe my player marker was also moving along the Z axis, which was affecting the speed based on how close I was to my player marker when I last clicked (set new destination).

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

144 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

Related Questions

Object jitters when the scene starts 0 Answers

Rigidbody2D.Velocity = Vector2 not changing X Position 1 Answer

slide and dash 2D 0 Answers

How do I Control An Objects Movement Only On the X Axis and Have a RigidBody Control Movement on the Y axis? 1 Answer

Vector2.MoveTowards and Rigidbody2D Velocity 3 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