- Home /
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);
}
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.
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
Your answer
Follow this Question
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
unity 2D with delay run and jump 0 Answers