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 polan31 · Sep 21, 2018 at 01:48 PM · c#movement2d gamelistpathfollowing

How to move along a few path follow ?

How to make the object after pressing the left mouse button move along the first path follow, stopped at the end and after repeated pressing the button moved the second path follow and etc.

I guess I should do it using the list.

I always have a big problem with the lists, I do not understand them completely, that's why I am asking for help.

I thought to create a path follow list, which object must pass.

Then give each of the path an index number.

And in the code for the object, write that it must pass them all in the correct order.

After passing the first path, it stops and waits until I press the left mouse button to go through the next one.

I do not know how to write it.

PS. I will give 15 reputation points for the best answer :)

My moving script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PathFollower1 : MonoBehaviour {
 
     public float speed = 3f;
     public Transform pathParent;
     Transform targetPoint;
     int index;
     bool rightClicked = false;
 
     public float speedRotate = 7f;
     Vector3 distance;
     Quaternion newRotate;
 
     // Use this for initialization
     void Start () {
         index = 0;
         targetPoint = pathParent.GetChild (index);
 
     }
 
     void Update () {
         if (Input.GetMouseButtonDown (0)) {
             rightClicked = true;
         }
         if(rightClicked)
             Move ();
 
     }
     
     // Update is called once per frame
     void Move() {
         transform.position = Vector3.MoveTowards (transform.position, targetPoint.position, speed * Time.deltaTime);
         rotate ();
         if (Vector3.Distance (transform.position, targetPoint.position) < 0.1f)
         {
             index++;
             index %= pathParent.childCount;
             targetPoint = pathParent.GetChild (index);
     }
 }
     void rotate ()
     {
 
         distance = transform.position - targetPoint.position;
         newRotate = Quaternion.LookRotation (distance, transform.forward);
         newRotate.x = 0;
         newRotate.y = 0;
         transform.rotation = Quaternion.Lerp (transform.rotation, newRotate, speedRotate * Time.deltaTime);
 
 }
 }
 


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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · Sep 22, 2018 at 12:56 AM

Good day.

First, i dont understand what the object have to do... ehn click once the left mouse button, it starts moving to next path? or you need to click multiple times to make if advance? The first point do it in oneclick way and the 2nd point in the multiclick way? then what?

You are not clear. I will do it to make one click to continue to the next point.

I will not give you the code. Thats your job to write it understanding what are you doing. I will give you the direction.

I think, the best way is follow this logic: First have 1 bool variable to know when the object is waiting for a click to continue. Then one integrer variable to know whats the actual path point (will be the index iin the path points list)

Ad of course, a list or array (if pathpoints will not change during gameplay, arrays is better) with the Vector3 positions of the pathpoints, or GameObjects and acces its transform.position.

then simply in Update, do 3 possible situations.

 if (Ready & Input.GetKeyDown(KeyCode.Mouse0)
 {
 ActualPathPoint++;
 Ready = false;
 }
 
 if (!Ready && Vector3.distance(object.transform.position, PathPointList[ActualPathPoint]) >=5)
 {
 Move(to PathPointList[ActualPathPoint]);
 }
 
 if (!Ready && Vector3.distance(object.transform.position, PathPointList[ActualPathPoint]) <5)
 {
 Ready = true;
 }

To make the array of PathPoints, make a public array and asign all points via inspector

 public GameObject PathPoints[];

You will need to check what distance you want to make the object change state (maybe 5 is too small, or too large)

Thats all!

Bye!

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

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

Making a bubble level (not a game but work tool) 1 Answer

MoveTowards on paths, different speeds 1 Answer

Move 2D Sprite Horizontally With Mouse 1 Answer

Multiple enemy prefabs moving towards a single goal. Need help! 1 Answer

MoveTowards on paths, different speeds 1 Answer


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