LegitTeddyBears

TrackingSystem1

Jun 2nd, 2017
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.01 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class TrackingSystem : MonoBehaviour {
  6.     public float speed = 20.0f;
  7.     public GameObject m_target = null;
  8.     Vector3 m_lastKnownPosition = Vector3.zero;
  9.     Quaternion m_lookAtRotation;
  10.     // Update is called once per frame
  11.     void Update ()
  12.     {
  13.         //This is short hand for if(m_target != null)
  14.         if (m_target)
  15.         {
  16.             if (m_lastKnownPosition != m_target.transform.position)
  17.             {
  18.                 m_lastKnownPosition = m_target.transform.position;
  19.                 m_lookAtRotation = Quaternion.LookRotation(m_lastKnownPosition - transform.position);
  20.             }
  21.  
  22.             if (transform.rotation != m_lookAtRotation)
  23.             {
  24.                 transform.rotation = Quaternion.RotateTowards(transform.rotation, m_lookAtRotation, speed * Time.deltaTime);
  25.             }
  26.         }
  27.     }
  28.  
  29.     public void SetTarget(GameObject target)
  30.     {
  31.         m_target = target;
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment