• 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
1
Question by calbar · Sep 24, 2013 at 12:09 AM · rigidbodycolliderpositionfollow

How can I get a rigidbody to follow my cursor without penetrating through colliders?

I'm trying to get a rigidbody to follow a target point (controlled by the mouse) that is moving around inside an enclosed mesh collider for my level. If the target point leaves the mesh collider, I need to guarantee that the rigidbody stays inside. I.e. if it can't reach the target point, it calmly presses against the mesh collider until it can.

I tried using rigidbody.MovePosition, but the rigidbody "bleeds through" walls it pushes up against, then starts vibrating violently and finally penetrates right through when the target point moves outside the mesh collider.

I tried implementing my own version of "MovePosition" by calculating the rigidbody's velocity to match the target point's position, but there was near-identical behavior as MovePosition.

Is there some way I haven't thought of to achieve what I'm after? Thanks!

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

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by calbar · Sep 29, 2013 at 07:47 PM

I stuck with adjusting the velocity according to mouse displacement and switched Collision Detection to Continuous, which solved my issues with penetration. A simplified version of my movement code:

rigidbody.velocity = ( ( transform.right * mouseMovement.x ) + ( transform.forward * mouseMovement.y ) ) / Time.deltaTime;

The rigidbodies still have an ugly jitter when they collide with walls, especially in corners. I've smoothed this out by averaging the last couple frames of motion, so while it feels slightly mushy, at least it's smooth and not going through walls.

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 tfishell · Oct 18, 2020 at 01:08 AM 0
Share

I'm not sure if too much time has passed on this question for me to reasonably expect an answer, but can you explain what you meant by "averaging the last couple frames of motion"?

avatar image
0

Answer by Nexxiom · Aug 31, 2016 at 07:54 AM

             Collider2D col1,col2;
             col1 = GameObject.Find("objectname").gameObject.GetComponent<Collider2D>();
             col2 = GameObject.Find("objectname").gameObject.GetComponent<Collider2D>();
             Physics2D.IgnoreCollision(col1, col2, true);

This works great! @calbar

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
5

Answer by DanielKhoroshko · Mar 15, 2015 at 11:24 AM

After a few hours of pain I went to this simple code

  Vector3 destintaion; //you fetch it in advance
  float sens = 10; //sensetivity
  
  GetComponent<Rigidbody>().velocity = (destination - transform.position) * sens;
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
1

Answer by robertbu · Sep 24, 2013 at 01:00 AM

How about upping the drag some and then using AddForce() to move the object in the direction of the cursor. I just ran a quick test with a sphere inside four wall with the following code, and it worked fine:

 #pragma strict
 
 function Update () {
     var pos = Input.mousePosition;
     pos.z = -Camera.main.transform.position.z;
     pos = Camera.main.ScreenToWorldPoint(pos);
     var dir = pos - transform.position;
     rigidbody.AddForce(dir * 10);
 }
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 calbar · Sep 29, 2013 at 07:29 PM 0
Share

Thanks for your reply robertbu.

Unfortunately, I've found AddForce to be really unwieldy for getting precise, controllable results. It always ends up with me experimenting with values until it just "looks right", and in some cases this is totally O$$anonymous$$!

But in this case, trying to get my object to follow mouse movements closely resulted in a lot of lagging behind and over-shooting, even when using the VelocityChange force mode for some reason.

It's entirely possible my understanding sucks or I was too impatient to figure out the math, but I opted for adjusting the velocity directly. Feel free to chime in if there's anything I may have missed or misunderstood. :)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

Camera rotation around player while following. 6 Answers

Kinematic Rigidbody Collider not colliding with Static Trigger Collider 3 Answers

rigidbody It's going up and bounces when is moving 1 Answer

Problem with Unity Terrain and GameObject 0 Answers

When jump my character controller model it goes out of collider 0 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