Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by kevinrocks_786 · Jan 24, 2016 at 07:16 PM · c#unity5classnavigationpath

How to make a gameobject go to certain points without animation

So I made a "Path Class" and it's to give a transform a bunch of Vector3's and it has to go there one by one. I can easily do this in animation. (Making an object move around to certain points) I have no idea what to do though.

Here are my classes:

 [System.Serializable]
     public class Path : System.Object
     {
         public Vector3[] pathDirecions;
 
         public Path (params Vector3[] directions)
         {
             this.pathDirecions = directions;
         }
 
 
     }
 
 
     static class PathExtensions
     {
         public static void FollowPath (this Transform obj, Path _path, float speed)
         {
             
         }
     }


What I have in mind is to use Vector3.MoveTowards() for each point in the Path.

Here is how I would like to call it.

     public Transform Player;
     public Path guide;
 
     void Start ()
     {
         Player.FollowPath(guide, 5); 
     }
 
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Xarbrough · Jan 24, 2016 at 09:54 PM

I would do something along these lines:

 using UnityEngine;
 
 namespace WaypointSystem
 {
     public class PathTest : MonoBehaviour
     {
         public Transform player;
         public Path path;
         public float speed = 1f;
 
         private Vector3 currentTarget;
 
         private void Start()
         {
             currentTarget = path.GetNextWaypoint();
         }
 
         private void Update()
         {
             player.position = Vector3.MoveTowards(player.position, currentTarget, speed * Time.deltaTime);
             if (player.position == currentTarget)
                 currentTarget = path.GetNextWaypoint();
         }
     }
 
     [System.Serializable]
     public class Path
     {
         [SerializeField]
         private Vector3[] nodes;
 
         private int currentNodeIndex;
 
         public Vector3 GetNextWaypoint()
         {
             Vector3 nextTarget = nodes[currentNodeIndex];
             currentNodeIndex = (currentNodeIndex + 1) % nodes.Length;
             return nextTarget;
         }
 
         public void Reset()
         {
             currentNodeIndex = 0;
         }
     }
 }

Be aware, that there's more to it, to make everything functional in different contexts. You want to think about who actually animates, the object itself or some other "animator" class etc. Instead of player.position == currentTarget you'd probably better check if the distance between them is smaller than a minimum threshold, or else you might miss the target point. Also some logic to protect the current animation from being broken while it's running, making sure the index is always correct or maybe instead of an index, calculate a waypointdistance between the start and end of the path etc.

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 EAssassinator · Jan 24, 2016 at 09:56 PM

So I think you are asking about a waypoint-like system. In this script you specify what points you want your object to go to by assigning the different points to the point array. Hope this helps! :)

 using UnityEngine;
 using System.Collections;
 
 public class WaypointScript : MonoBehaviour {
 
     //How fast we travel
     public float speed =.5f;
     //The different points we need to travel to
     public Vector3[] points;
     //Which value in our objective array we are on
     int pointValue;
     //Do we allow some give on how close we need to be to the objective?
     public bool isLieneant = false;
     //If so how far away can we be before it's not close enough?
     public Vector3 lineance;
 
     bool shouldMove = true;
     
     void Update() {
         
         if(shouldMove == true) {
             
             if(points.Length == 0) {
                 Debug.LogWarning("There are no points assigned!");
                 shouldMove = false;
                 return;
             }
             if(pointValue == points.Length) {
                 shouldMove = false;
                 Debug.LogWarning("We have completed all objectives!");
                 return;
             }
             
             transform.position = Vector3.MoveTowards(transform.position, points[pointValue], (speed * Time.deltaTime));
             
             if(isLieneant == false) {
                 
                 if(transform.position.x >= points[pointValue].x && transform.position.y >= points[pointValue].y && transform.position.z >= points[pointValue].z) {
                     
                     pointValue++;
                     
                 } 
             }
             
             //If we allow it to be lieneant then we should check for our values with the lineance subtracted from them
             else if (transform.position.x >= points[pointValue].x - lineance.x && transform.position.y >= points[pointValue].y - lineance.y && transform.position.z >= points[pointValue].z - lineance.z) {
                 
                 pointValue++;
                 
             }
         }
     }
 }

P.S It's been quite some time since I last used C# so sorry if it looks bad or confusing

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

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

Programmmed Animation (making transform move to vectors with code) 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

NavMeshAgent dont find the right way on runtime build NavMesh ,Agents didn´t walk right on a runtime generated NavMesh 0 Answers

unity 2D with delay run and jump 0 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