- Home /
Make an object move toward a point, but stop when it hits another object?
I'm trying to make something that allows you to grab an object and it will gradually slide toward the mouse which I have done using Vector3.Lerp. This part works fine. I want the object to stop moving when it hits an obstacle in between it and the mouse point but still be able to move around it should the position of the mouse change.
In other words, if an object is moving toward the mouse and an obstacle blocks it, the object should sort of start to slide around the obstacle.
I have no idea how I can go about doing this.
Answer by Blacklink2001 · Aug 08, 2016 at 07:12 PM
You could try using a simple collider. Add colliders to the object moving towards the mouse and colliders to the object that should block it. Then you should add a function on the object moving towards the mouse saying something like
public bool Colliding = false; void OnCollisionEnter (Collision col) { %|-388435161_2|% %|-937706317_3|% %|949459639_4|% %|258082576_5|% } Then change the part that is moving it towards the mouse by adding an if(!Colliding) statement. The first part will keep track of when the object has hit another one, and by adding the if statement you can make it stop the movement when colliding is true. This might be very buggy but it should work.
Oh sorry only just noticed how old this post is... This is probably never going to be noticed.
Answer by villevli · Aug 08, 2016 at 07:13 PM
You should use Unity's physics components and use rigidbody.AddForce to move the object towards the mouse. The object and the obstacle both need colliders. The object also needs a Rigidbody so it can be moved by Unity's physics system. (Use the 2D variants of the components if your game is 2D). Depending on your game you may need to disable gravity in the rigidbody or in the physics manager. You can use this method to make AddForce work more like adjusting the transform position with Vector3.Lerp
Answer by nathanthesnooper · Aug 08, 2016 at 07:14 PM
Haven't tried it yet, but looks like its good.
Not doing these in editor so fix basic errors :X
if you want the simplest form of "pathfinding" (Just move towards an object). you can either be lazy with bad physics using
Vector3.MoveTowards(this.transform.position, Vector3 Target, float Speed);
or... Vector3 Force = Vector3.zero; Vector3 TmpForce = Vector3.MoveTowards(this.transform.position, Vector3 Target, float Speed); Force = new Vector3(TmpForce.x - this.transform.position.x, TmpForce.y - thi.... ....position.z); Rigidbody.velocity = Vector3.zero; Rigidbody.angularvelocity = 0; Rigidbody.Addforce(Force);
and if your fine with rotation
follow this
and
Rigidbody.Addforce(transform.forward); or in 2D Rigidbody2D.Addforce(transform.up);
:)
Your answer
Follow this Question
Related Questions
move around script help me 4 Answers
How to make an AI that moves around 1 Answer
How do I make the nose/whole of my spaceship move? 2 Answers
Camera move belong a certain path 1 Answer