- Home /
Pointing towards Waypoint
i need help with my waypoint system! I didnt make this script and i dont know what to change. right now the character goes towards the waypoint perfectly, but i need him to POINT towards the next waypoint. is there a way to do this? here is the script:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Waypoint : MonoBehaviour { public GameObject[] waypoints; int current = 0; float rotspeed; public float speed; float WPradius = 1;
void Update()
{
if (Vector3.Distance(waypoints[current].transform.position, transform.position) < WPradius)
{
current++;
if (current >= waypoints.Length)
{
current = 0;
}
}
transform.position = Vector3.MoveTowards(transform.position, waypoints[current].transform.position, Time.deltaTime * speed);
}
}
Answer by deniskotpletnev · Jul 27, 2020 at 09:25 PM
And you've added to the waypoints array (public GameObject [] waypoints;) in the Unity editor additional points?
Answer by UnityedWeStand · Jul 28, 2020 at 12:21 AM
Add this line in your Update(), assuming your character's forward direction is its local positive z axis.
transform.rotation = Quaternion.LookRotation(waypoints[current].transform.position - transform.position)
Your answer
Follow this Question
Related Questions
How to change enemy's Direction on FiniteStateMachine??? 1 Answer
AI faces Weird Direction when following a waypoint 0 Answers
City traffic Lanes 3 Answers
Unity Car tutorial, Wrong way code 0 Answers
Waypoint System Does Not Rotate Enemy? 2 Answers