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
1
Question by zefrof · Aug 22, 2012 at 07:02 PM · movementtransformarrayfunctiontranslate

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.

Comment
Add comment · Show 1
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 Karsnen_2 · Mar 14, 2013 at 09:57 PM 1
Share

I would rather suggest Go$$anonymous$$it from Prime31 than itween from pixelplacement.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by BLarentis · Aug 22, 2012 at 10:10 PM

Hello,

i suggest using iTween, really easy to make paths:

itween.pixelplacement.com

Take care!

Comment
Add comment · Show 3 · 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 zefrof · Aug 22, 2012 at 11:06 PM 0
Share

I'm pretty new to c# and coding is general. How exactly would I go about this?

avatar image BLarentis · Aug 23, 2012 at 12:48 AM 0
Share

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!

avatar image zefrof · Aug 23, 2012 at 02:52 PM 0
Share

Got everything working. Thanks for all the help!

avatar image
0

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?

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
avatar image
0

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];
 }

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

11 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

Related Questions

Moving multiple transforms from an array in a single script 0 Answers

How to translate a camera forward and back(basically a zoom) according to the distance of two objects? 1 Answer

How do I modify the current transform? 2 Answers

Simple C# Vector2D Moving Question 1 Answer

How can I move an object a certain distance, once? 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