- Home /
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);
}
}
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!