I am a beginner and was using tutorials on yt to make waypoints. I just noticed a big problem. Since i was watching a tutorial for 3d core, there is something that went wrong in my code. For smooth rotation I used Quaternion and lookdirection but i think because of that my enemy is rotating 90 on y axis making the enemy invisble. What can i do to correct my script. Here’s my waypointmover script,
Please help me solve the problem T-T
public class WaypointMover : MonoBehaviour
{
//Stores a reference to the waypoint system this object will use
[SerializeField] private Waypoints waypoints;
[SerializeField] private float moveSpeed = 2f;
[Range(0f, 15f)] //How fast enemy will rotate once it reaches its waypoint
[SerializeField] private float rotateSpeed = 4f;
[SerializeField] private float DistanceThreshold = 0.1f;
//Current waypoint target that the object is moving towards
private Transform currentWaypoint;
//The rotation target for current frame
private Quaternion rotationGoal;
//The direction to the next waypoint that the enemy needs to rotate to
private Vector3 directionToWaypoint;
// Start is called before the first frame update
void Start()
{
//Set initial position to the first waypoint
currentWaypoint = waypoints.GetNextWaypoint(currentWaypoint);
transform.position = currentWaypoint.position;
//Set the next waypoint target
currentWaypoint = waypoints.GetNextWaypoint(currentWaypoint);
transform.LookAt(currentWaypoint);
}
// Update is called once per frame
void Update()
{
transform.position = Vector3.MoveTowards(transform.position, currentWaypoint.position, moveSpeed * Time.deltaTime);
if (Vector3.Distance(transform.position, currentWaypoint.position) < DistanceThreshold)
{
currentWaypoint = waypoints.GetNextWaypoint(currentWaypoint);
//transform.LookAt(currentWaypoint);
}
RotateTowardsWaypoint();
}
//Will slowly rotate the enemy towards the current waypoint it is moving towards
private void RotateTowardsWaypoint()
{
directionToWaypoint = (currentWaypoint.position - transform.position).normalized;
rotationGoal = Quaternion.LookRotation(directionToWaypoint);
transform.rotation = Quaternion.Slerp(transform.rotation, rotationGoal, rotateSpeed * Time.deltaTime);
}
}