Moving a GameObject on a square patern
My goal is to move a GameObject on a square patern, in order to do it I made this script: public class SquarePatern : MonoBehaviour {
public Transform target;
public Transform target1;
public Transform target2;
public Transform target3;
public Transform target4;
public float speed;
void Update()
{
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.name == "Target1")
{
target = target2;
}
if (col.gameObject.name == "Target2")
{
target = target3;
}
if (col.gameObject.name == "Target3")
{
target = target4;
}
if (col.gameObject.name == "Target4")
{
target = target1;
}
}
}
I ensured that the 4 transform have a box Collider and have the same name as in the script, but somehow it just doesn't work, the GameObject goes to the target1 and stop on it, what did i do wrong?
Comment
Your answer
Follow this Question
Related Questions
Player trigger on platform with prefab 0 Answers
Destroy GameObject inconsistent with Instantiated children 1 Answer
How to move an object on a terrain that will always stay on top of the terrain? 2 Answers
How can i put down a sprite on collision? 0 Answers
How to show an object when collided with 0 Answers