Advertisement
LeeMace

Pathfinding follow

Apr 19th, 2024
511
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | Gaming | 0 0
  1. public class PathFinder : MonoBehaviour {
  2.     public Transform target;
  3.     public float speed = 5f;
  4.  
  5.     void Update() {
  6.         // The direction is calculated by subtracting the target's position from the current position of the object, then normalizing the result.
  7.         Vector3 direction = (target.position - transform.position).normalized;
  8.         // The angle is calculated using the direction vector, then converted to degrees and applied to the object's rotation.
  9.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
  10.         // The object's position is updated based on the direction and speed, and the rotation is set to face the target.
  11.         transform.rotation = Quaternion.Euler(0, 0, angle);
  12.         // The object's position is updated based on the direction and speed, scaled by the time since the last frame.
  13.         transform.position += direction * speed * Time.deltaTime;
  14.     }
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement