Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by BobMan · Feb 06, 2015 at 07:55 AM · follow path

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!

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Phles · Feb 06, 2015 at 09:20 AM 0
Share

try changing transform.Translate(...) to..

 transform.position += wayPointDirection * (Time.deltaTime * speed);
avatar image BobMan · Feb 07, 2015 at 11:25 PM 0
Share

thx but it still doesn't work. Nothing happens, the enemy doesnt move. If anyone can help, please do.

avatar image SnStarr · Feb 08, 2015 at 12:19 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
11

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];
         }
     } 
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image soundmartell · May 23, 2016 at 04:20 PM 0
Share

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.

avatar image Gotal soundmartell · Oct 03, 2016 at 09:36 AM 0
Share

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

avatar image alteromonique · Sep 18, 2018 at 01:16 PM 0
Share

Hi, i`m getting ArrayOutOfIndex exception here. Any idea?

avatar image seniorfuzzyhead · Apr 23, 2019 at 08:38 PM 0
Share

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.

avatar image oneted_7 · Mar 01, 2020 at 08:47 PM 0
Share

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....

avatar image
0

Answer by gOzaru84 · Dec 13, 2018 at 11:06 AM

https://www.youtube.com/watch?v=1aBjTa3xQzE This is the answer.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges