Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class PathFinder : MonoBehaviour {
- public Transform target;
- public float speed = 5f;
- void Update() {
- // The direction is calculated by subtracting the target's position from the current position of the object, then normalizing the result.
- Vector3 direction = (target.position - transform.position).normalized;
- // The angle is calculated using the direction vector, then converted to degrees and applied to the object's rotation.
- float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
- // The object's position is updated based on the direction and speed, and the rotation is set to face the target.
- transform.rotation = Quaternion.Euler(0, 0, angle);
- // The object's position is updated based on the direction and speed, scaled by the time since the last frame.
- transform.position += direction * speed * Time.deltaTime;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement