Reputation: 6552
I'm trying to move my enemy mob back to it's starting location but it's going somewhere no even remotely close to it's starting position. Here is what I have
private Vector3 startingPosition;
void Start ()
{
startingPosition = transform.TransformPoint(Vector3.zero);
}
In the update position I do some checks to make sure when we lose chase range for the mob to run back to it's starting location with this. Well I'm trying to get it to run back to it's starting location
void Return2Location()
{
Debug.Log("Start" + startingPosition);
Debug.Log("Current " + transform.position);
if (transform.position != startingPosition)
{
Debug.Log("returning");
controller.Move(startingPosition);
animation.CrossFade(runClip.name);
}
}
If i set transform.position to startingPosition it's placed back to it's original location.
Update
Tried this also
Transform newLocation = new GameObject().transform;
newLocation.position = startingPosition;
newLocation.rotation = startingRotationPosition;
controller.SimpleMove(newLocation.forward * speed);
Upvotes: 0
Views: 276
Reputation: 20028
The problem there seems to be your use of Move()
. Move takes a (scaled) direction as an argument. Not a position it needs to go to. So if you want to move it towards your starting position, you'll have to do something like the following instead:
Vector3 direction = startingPosition - Transform.position;
direction.Normalize(); //Make sure to have a pure "unit" direction
controller.Move(direction * speed * Time.deltaTime);
That should take you in the right direction at your desired speed. Of course you'll have to make sure you don't overshoot your target, but you get the idea.
Upvotes: 1