- Home /
How would I move an object towards the mouse but not stick to it.
I want to move a circle around with my mouse but I dont want it to go through walls, how would I achieve this? For example if its in a box and my mouse moves outside the box, the circle should stay inside the box and not clip through it.
Answer by Captain_Pineapple · Jan 14 at 07:57 AM
Hey there,
make your circle an object with a collider and a rigidbody. Then in your Update make the circle move towards the mouse position with GetComponent<Rigidbody>().MovePosition(Vector3.MoveTowards(circlePos, mousePos, speed*Time.deltaTime))
This should then take care of collision detection for you.
Let me know if that helped you or if anything is wrong here. Note that i did not test this and you have to adjust variable names and stuff.
Thank you! I was thinking of doing this but didnt know how to do it.
Your answer
![](https://koobas.hobune.stream/wayback/20220613051631im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to move objects away from each other (not what you think) 1 Answer
How to make a little wiggle room for the collision? 1 Answer
Best way to manually calculate collision between GameObjects and a Tilemap without using physics 1 Answer
2D Triggers and Collisions not working as expected. 2 Answers
Different platform effector 2d rotations for different colliders 1 Answer