Movement Script Help

I’m not that good at programming, but I attempted to make a script that makes a game object go to one transform and then another and it will travel in a triangle over and over again. When I made the script, the game object went towards the first transform and just circled around it. How can I make it so then the game object travels in a triangle? Here is my script:

var moveSpeed = 5;
var targetOne : Transform;
var targetTwo : Transform;
var targetThree : Transform;
var damping = 2;

function Update(){
	if(gameObject.transform.position == targetOne.position){
		Invoke("lookAt", 0.0);
		Invoke("attack", 0.0);
	}
	if(gameObject.transform.position == targetTwo.position){
		Invoke("lookAtTwo", 0.0);
		Invoke("attack", 0.0);
	}
	if(gameObject.transform.position == targetThree.position){
		Invoke("lookAtThree", 0.0);
		Invoke("attack", 0.0);
	}
			
}
function lookAt ()
{
var rotation = Quaternion.LookRotation(targetTwo.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}

function attack ()
{
 
    transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
}

function lookAtTwo ()
{
var rotationTwo = Quaternion.LookRotation(targetThree.position - transform.position);
transform.rotationTwo = Quaternion.Slerp(transform.rotationTwo, rotationTwo, Time.deltaTime * damping);
}

function lookAtThree ()
{
var rotationThree = Quaternion.LookRotation(targetOne.position - transform.position);
transform.rotationThree = Quaternion.Slerp(transform.rotationThree, rotationThree, Time.deltaTime * damping);
}

The problem I believe you are having is that you are checking for when your position is EXACTLY equal to the targets position. This provides such a little amount of room for error, that your dampened rotation makes you circle around the target, but not actually get close enough to it to trigger the next target. The best way to avoid this happening would be to check when you are within a certain amount of distance to your current target.

For Example:

if((transform.position - target.position).magnitude < 1f) {
    Invoke("LookAt" 0.0);
    Invoke("Attack" 0.0);
}

The way that I would go about this is a little bit different from yours. Here is my code:

using UnityEngine;
using System.Collections;

public class ExampleMovement : MonoBehaviour {
	//Here I am defining a new array called "targets", with an array length of 3.
	//This means that there will be 3 transforms in my array.
	//I can assign these in the inspector window.
	public Transform[] targets = new Transform[3];
	//Here I am defining an int variable which i can use to change the current "targets" transform to use.
	public int curTarget = 0;
	
	void Update() {
		//Check if we are within 1 unit of our target (reduces risk of error - object could miss target which is what causes yours to circle the target)
		if((transform.position - targets[curTarget].transform.position).magnitude < 1f) {
			//When we are within one unit of target, switch to the nex target(or the next transform in the array).
			curTarget++;
			//When our current target reaches the length of the array, we will loop it back to 0 so that it repeats.
			if(curTarget > 2) {
				curTarget = 0;
			}
		}
		//Move forwards
		transform.Translate(Vector3.forward * 3.0f * Time.deltaTime);
		//Always look at the current target
		transform.LookAt(targets[curTarget].position);
	}
}

Sorry that is is C#, but i have commented it heavily to make it easy to understand.

Hope this helps :wink: