- Home /
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.
I would rather suggest Go$$anonymous$$it from Prime31 than itween from pixelplacement.
Answer by BLarentis · Aug 22, 2012 at 10:10 PM
I'm pretty new to c# and coding is general. How exactly would I go about this?
Hello, download the iTween package (via asset store) and download the visual path editor. Import it inside your Unity project. Then create an empty game object inside your scene. Drag the script iTweenPath to this game object. At your Inspector window, when selecting this game object there will appear some options. There you can select the number of points that your path will follow. At your scene window you will see those points and there you will be able to freely move them and create the path as you like. You will need to give a name to this path also, here let´s call it "EnemyPath1".
After you create your path, you will have to make your object walk through it. To do that use this command inside your Enemy$$anonymous$$ovement script:
void Start () {
UpdatePosition = true;
DelayUpdatePos = 1;
FirstYPos = 5;
$$anonymous$$ovementSpeed = 5;
//Here i'm telling to the owner of this script to wall by the path created with the name "EnemyPath1"
iTween.$$anonymous$$oveTo(this.gameObject,iTween.Hash("path",iTweenPath.GetPath("EnemyPath"),"time",3));
}
This way your enemy will walk all this path in 3 seconds. You can change this time, changing the value after "time" inside iTween.Hash. After he complete the path he will stay there. You can set it as a loop, adding the field "looptype", iTween.LoopType.loop(it will appear a list of loop types, i don´t remember if the correct name is loop, just check it).
$$anonymous$$ake sure to look at the iTween site to see more options to use inside the iTween.Hash.
Take care!
Answer by LlamaNervous · Aug 12, 2013 at 07:40 AM
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 :/ Anyone be able to help me out?
Answer by Glurth · Dec 30, 2014 at 05:57 PM
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];
}