How to have a GameObject follow a path with c#?

I am trying to get a GameObject to follow a path as I stated above. Based off my little bit of knowledge I believe the best way to create a path would be an array. First question, am I right is an array the best way? Next here’s my current script:

using UnityEngine;
using System.Collections;

public class EnemyMovement : MonoBehaviour {
	
	public bool UpdatePosition;
	
	public float DelayUpdatePos;
	public float FirstYPos;
	public float MovementSpeed;
	
	public GameObject BasicEnemy;
	
	// Use this for initialization
	void Start () {
		UpdatePosition = true;
		DelayUpdatePos = 1;
		FirstYPos = 5;
		MovementSpeed = 5;
	}
	
	// Update is called once per frame
	void Update () {
		
		if(UpdatePosition == true && DelayUpdatePos == 0) {
				BasicEnemy.transform.Translate(MovementSpeed, 0, 20, 0);
		}
		
		if(DelayUpdatePos > 0) {
			DelayUpdatePos -= Time.deltaTime; 
		}
		
		if(DelayUpdatePos < 0) {
			DelayUpdatePos = 0;	
		}
	}
}

Now if I’m correct I need to change the BasicEnemy.transform.Translate(MovementSpeed, 0, 20, 0); line in my code. I’m not 100% positive if "Translate is the correct function. If it is the correct function what do I need to put in the parentheses that’ll cause the GameObject to follow a array(?) at a certain speed? If it not the correct function what is? I’ve looked at the unity script reference and did some google searches but I haven’t found anything.

NOTE: Yes I realize there isn’t an array yet.

Hello,

i suggest using iTween, really easy to make paths:

itween.pixelplacement.com

Take care!

Hello, I am completely new to Unity so I have limited knowledge. Basically I have an enemy tank that I want to follow a small path that loops. I have no idea how to set a path or array or spline and I can’t find any tips on here :confused: Anyone be able to help me out?

First question, am I right is an array the best way?

I guess this depends on what the array contains. Simple position vectors might not be the best, I would suggest you store an array of spline parameters instead. This will make movement smoother and more flexible, but this is obviously alot more complex;

Now if I’m correct I need to change the BasicEnemy.transform.Translate(MovementSpeed, 0, 20, 0); line in my code. I’m not 100% positive if "Translate is the correct function. If it is the correct function what do I need to put in the parentheses that’ll cause the GameObject to follow a array(?) at a certain speed? If it not the correct function what is? I’ve looked at the unity script reference and did some google searches but I haven’t found anything.

Your statement uses your movement speed to change ONLY the x axis- I’m not quite sure why. I would do it something like this (untested.)

Vector3[] Waypoint;   //I'll assume you have this stuff already setup with a list of 3D coordinates in the code below

float faction_of_path_traveled;
int lastWaypoint,nextWaypoint,finalWaypoint;
//...
void Update()
{
   if(nextWaypoint > finalWaypoint) return;
   Vector3 fullPath=Waypoint[lastWaypoint]-Waypoint[nextWaypoint]; //defines the path between lastWaypoint and nextWaypoint as a Vector3
   faction_of_path_traveled+=MovementSpeed * Time.deltaTime; //animate along the path
   if(faction_of_path_traveled>1) //move to next waypoint
   {
      lastWaypoint++;nextWaypoint++;
      
      faction_of_path_traveled=0;
      Update();
      return;
   }
   //we COULD use Translate at this point, but it's simpler to just compute the current position
   BasicEnemy.transform.position= (fullPath * faction_of_path_traveled) + Waypoint[lastWaypoint];
}