duck

Unity 3D Rotate towards target around one local axis

Apr 22nd, 2013
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.50 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Turret : MonoBehaviour {
  5.    
  6.     public Transform target;
  7.     public float rotateSpeed = 45;
  8.    
  9.     // Update is called once per frame
  10.     void Update () {
  11.    
  12.         var localTarget = transform.InverseTransformPoint(target.position);
  13.         localTarget.y = 0;
  14.         var targetRot = transform.rotation * Quaternion.LookRotation( localTarget );
  15.         transform.rotation = Quaternion.RotateTowards( transform.rotation, targetRot, Time.deltaTime * rotateSpeed );
  16.        
  17.     }
  18. }
Advertisement
Add Comment
Please, Sign In to add comment