Advertisement
Guest User

Untitled

a guest
Aug 23rd, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.56 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class ExampleClass : MonoBehaviour
  5. {
  6.     public Transform target;
  7.     public float maxHorizontalDistance = 10;
  8.  
  9.     void Update()
  10.     {
  11.     if(target == null) return;
  12.  
  13.         var distance = target.position - transform.position;
  14.         distance.y = 0;
  15.         var magnitude = distance.magnitude;
  16.         if(magnitude > maxHorizontalDistance)
  17.         {
  18.            transform.position += distance.normalized * (magnitude - maxHorizontalDistance);
  19.         }
  20.  
  21.         transform.LookAt(target);
  22.     }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement