# Moving towards and switching targets in Unity with collision

I have these nodes set up for a ship to move towards over time with out looking stops then repeats in a sequence.

like so:

1 2 3 1 2 3 1 2...

THE PROBLEMS i keep having either come from the inability to switch direction or the ship ignores all collision which i would like to be able to shoot it with actual objects.

Ignores all Collision:

Vector3 currentPosition = thisShip.transform.position;
Vector3 directionOfTravel = targetPosition - currentPosition;
directionOfTravel.Normalize();
thisShip.MovePosition(currentPosition + directionOfTravel * speed * Time.smoothDeltaTime);


This one works perfectly but because MovePosition is a kin to translate and actually teleports objects it ignores all collision which i need.

Can't Switch Direction:

Vector3 currentPosition = thisShip.transform.position;
Vector3 directionOfTravel = targetPosition - currentPosition;
directionOfTravel.Normalize();
thisShip.AddRelativeForce(currentPosition + directionOfTravel * speed * Time.smoothDeltaTime, ForceMode.Force);


so in this one i try to stop the rigid body by returning its velocity to zero and it does stop for a second but continues in its original direction anyways.

thisShip.velocity = Vector3.zero; // Use this to pause object


I believe i need another method of movement (maybe use a CC) or maybe i can make AddRelativeForce work and be able to switch targets like MovePosition does. I'd really like to get this one thing done so i can move on to other parts of the project.

My apologies if I'm wrong but.. Dude, drop that script and take a look at the tutorials, specially -Roll a Ball- and SpaceShooter

You're not checking for collisions you just posted the same code twice. The code for move your object have not sense: You're not applying forces or transforms. You're just adding and subtracting your current position to that mysterious targetPosition variable.

I'd never used MovePosition and yes I'm new in this so you're probably applying transformations, nevertheless, I'm pretty sure you don't know what you're doing. Am I right?

Remember that apply forces and transformation are not the same. Also I'm not sure if 'velocity' is deprecated and you should use the property from the component (rigidbody probably).

I guess you should drive your ship like a car but in the opposite way. Good luck, sorry I can't help but I can't wrote a comment

• I don't understand, did you edit the question? Because looks like you didn't, but this time have much more sense, in fact the code are not the same. Maybe my poor internet connection is kidding with me or the app itself. Or maybe I was not clear yesterday. Anyway, good for you your code works now. – Karmavil May 12 '16 at 23:09

well i fixed it. lol well the reason it wasn't working right was because i used translate on another part of the script to move the whole ship on a rail while this part moves side to side. so these few line of code weren't the problem at all. guess that's what i get for trying to be secretive and hiding code. MovePosition works fine with collision. but still don't know what was going on with addrelativeforce. Meh well i solved it anyways now i can finally move on in development