• 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
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
ahungrybear

People who like this

1 Show 0
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

  • Sort: 
avatar image
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
CoolCasualCat
ahungrybear

People who like this

2 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

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
chrisarton
keashi
LightPotato
Callaghan2132
AnaLuyza

People who like this

5 Show 0 · 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

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
VicM

People who like this

1 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 OK!

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. :)

avatar image

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

People who like this

0 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

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

Dose any one know how to send object current postion to preivues posistion 1 Answer

Rigidbody stuck in corners of Mesh Collider 0 Answers

How to prevent objects from interpenetrating eachother ? 1 Answer

Make a collider NOT collide with CharacterController 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