Simple waypoint follow AI in unity | Patrolling AI in unity3d | Waypoint system unity
Code :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyWayPoint : MonoBehaviour
{
public Transform[] allwayPoints;
public float rotationSpeed = .5f, movementSpeed = 0.5f;
public int currentTarget;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
Movement();
Rotate();
ChangeTarget();
}
void Movement()
{
transform.position = Vector3.MoveTowards(transform.position,allwayPoints[currentTarget].position,movementSpeed*Time.deltaTime);
}
void Rotate()
{
transform.rotation = Quaternion.Slerp(transform.rotation,
Quaternion.LookRotation(allwayPoints[currentTarget].position-transform.position),rotationSpeed*Time.deltaTime);
}
void ChangeTarget()
{
if (transform.position==allwayPoints[currentTarget].position)
{
currentTarget++;
currentTarget = currentTarget % allwayPoints.Length;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////
ASSALAM o alaikum
All codes and concepts and videos from this channel are non copyrighted and free to use because I believe that knowledge should be freely and easily available for everyone
/////////////////////////////////////////////////////////////////////////////////////////////