- Home /
How to make object follow path?
Hey I'm a noob here and I need help making my object follow a path. I've used waypoints and the nav bake function but none of them are working. I'v looked all around so and link you'll send me I would've probably already look through.
The problem with this script is that the thing that I want to follow the path doesn'e even move. In fact, nothing at all happens and I have no idea why.
This is my code if you were wondering:
using UnityEngine; using System.Collections;
public class WayPoints : MonoBehaviour {
public Transform[] wayPoint = new Transform[9];
int currentWayPoint = 1;
public float rotationSpeed = 6.0f;
public float acceleration = 1.8f;
// Use this for initialization
void Start () {
wayPoint [0] = GameObject.Find ("WayPoint1").transform;
wayPoint [1] = GameObject.Find ("WayPoint2").transform;
wayPoint [2] = GameObject.Find ("WayPoint3").transform;
wayPoint [3] = GameObject.Find ("WayPoint4").transform;
wayPoint [4] = GameObject.Find ("WayPoint5").transform;
wayPoint [5] = GameObject.Find ("WayPoint6").transform;
wayPoint [6] = GameObject.Find ("WayPoint7").transform;
wayPoint [7] = GameObject.Find ("WayPoint8").transform;
wayPoint [8] = GameObject.Find ("WayPoint9").transform;
}
// Update is called once per frame
void Update () {
if (currentWayPoint == 9) {
Destroy (this.gameObject);
Player.lives =- 1;
}
else
{
// walk(); } }
void walk(){
Quaternion rotation = Quaternion.LookRotation (wayPoint [currentWayPoint].position - transform.position);
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime * rotationSpeed);
Vector2 wayPointDirection = wayPoint[currentWayPoint].position - transform.position;
float speedElement = Vector2.Dot(wayPointDirection.normalized, transform.forward);
float speed = acceleration * speedElement;
transform.Translate (0, 0, Time.deltaTime * speed);
}
void onTriggerEnter(Collider collider){
if (collider.tag == "WayPoint") {
currentWayPoint ++ ;
}
}
}
thx for your time!
try changing transform.Translate(...) to..
transform.position += wayPointDirection * (Time.deltaTime * speed);
thx but it still doesn't work. Nothing happens, the enemy doesnt move. If anyone can help, please do.
No errors in the console of any kind? Or the enemy just doesn't move at all? I may be wrong here, but this:
transform.Translate (0, 0, Time.deltaTime * speed);
seems to be telling the enemy to move 0 on the x, 0 on the y, and then telling it how FAST to move on the Z, but not telling how FAR to move on the Z. If I am wrong I am sorry, it's just the way it looks to me.
Answer by sansol · Jun 29, 2015 at 03:55 PM
hi , i don't know if you are still looking for an answer, but since i had the same problem and found a solution to it, i post it here for future reference.
code here:
public class WayPoints : MonoBehaviour {
// put the points from unity interface
public Transform[] wayPointList;
public int currentWayPoint = 0;
Transform targetWayPoint;
public float speed = 4f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
// check if we have somewere to walk
if(currentWayPoint < this.wayPointList.Length)
{
if(targetWayPoint == null)
targetWayPoint = wayPointList[currentWayPoint];
walk();
}
}
void walk(){
// rotate towards the target
transform.forward = Vector3.RotateTowards(transform.forward, targetWayPoint.position - transform.position, speed*Time.deltaTime, 0.0f);
// move towards the target
transform.position = Vector3.MoveTowards(transform.position, targetWayPoint.position, speed*Time.deltaTime);
if(transform.position == targetWayPoint.position)
{
currentWayPoint ++ ;
targetWayPoint = wayPointList[currentWayPoint];
}
}
}
Hello, Thank you, This script solved some of my problems but I need some help. I am using it in a Plane so the plane will follow the wayPoint. In fact the plane follow the path but for some reason // rotate towards the target transform.forward = Vector3.RotateTowards(transform.forward, targetWayPoint.position - transform.position, speed*Time.deltaTime, 0.0f);
Rotate the Plane and the plane follow the path with the tail pointing to the WayPoint. So the plane is flying reverse. The Way Point and the Plane has the Axes in the same direction. I think it has to be something with the Word coordinates. Is it any way to correct this? Thank you very much for any help or support.
So i'm new here, dont know if you were able to solve your problem... what I would do is open your model in $$anonymous$$aya (or something) and rotate it 180 ° (and freeze transforms - del history).
Think this would be the simplest fix. Cheers
Hi, i`m getting ArrayOutOfIndex exception here. Any idea?
Hello I am also new to unity i have a question involving your solution. is the solution a complete program or a $$anonymous$$or script change. if i know this i could make a zombie horde game. please reply as soon as you can i would be grateful to know this. thank you.
sincerely SENIORFUZZYHEAD.
hey , i have alot enemy that i wanna have a simple move to right and back to left again and again .i know i can use points and objects for everyone of them to make a path and do this but is there anyway only with codes can do this ? (without any new object ) like this ....
transform a ;
a =new vector2(gameobject.transform.position.x +3f ,gameObject.transform.position.y);
b=new vector2(gameobject.transform.position.x -3f ,gameObject.transform.position.y);
transform.position = Vector2.$$anonymous$$oveTowards(gameObject.transform.position, a.transform.position, movespeed * Time.deltaTime);
if (gameObject.transform.position.x == a.transform.position.x)
transform.position = Vector2.$$anonymous$$oveTowards(a.transform.position, b.transform.position, movespeed * Time.deltaTime);
if (gameObject.transform.position.x == b.transform.position.x)
transform.position = Vector2.$$anonymous$$oveTowards(b.transform.position, a.transform.position, movespeed * Time.deltaTime);
already i did this but didnt work....
Answer by gOzaru84 · Dec 13, 2018 at 11:06 AM
https://www.youtube.com/watch?v=1aBjTa3xQzE This is the answer.
Your answer
Follow this Question
Related Questions
How can I Make a path for Camera ? 1 Answer
Rigidbody trying to follow a spline with smoothing 1 Answer
Character moves along pre-defined path / curve 1 Answer
Make an NPC follow player? 1 Answer
Collider is not working... 2 Answers