Hi ayks!
You should read about Vector3.MoveTowards and transform.LookAt...
Actually, this is the first time I see a moving system using rigidbody.MovePosition... When you are blocked, you should try another way.
What would I do? There is a lot of ways of doing it...
1. Create two empty objects named: Target1 and Target2 and put them where you want your monster to go;
2. Put this in monster script:
public GameObject target1, target2;
public float spd;
private GameObject myTarget;
void Start()
{
myTarget = target1;
}
// Update is called once per frame
void Update ()
{
if (transform.position == target1.transform.position)
{myTarget = target2;}
if (transform.position == target2.transform.position)
{myTarget = target1;}
transform.position = Vector3.MoveTowards (transform.position, myTarget.transform.position, spd * Time.deltaTime);
transform.LookAt (myTarget.transform);
}
3. Drag and drop the 2 targets.
What is cool with this, you can control the speed and the destinations. If you want more than two target, read about Arrays. Don't forget that with the RigidBody component, you can add constraint with position and rotation.
This should fix these bizarre behaviors!
Math
↧