Advertisement
Guest User

Untitled

a guest
May 21st, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.80 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class RotateTest : MonoBehaviour {
  6.  
  7.     public Transform Target;
  8.  
  9.     void Start () {
  10.        
  11.     }
  12.  
  13.  
  14.     void Update () {
  15.         transform.rotation = FaceObject(transform.position, Target.position, FacingDirection.RIGHT);
  16.     }
  17.  
  18.     public enum FacingDirection
  19.     {
  20.         UP = 270,
  21.         DOWN = 90,
  22.         LEFT = 180,
  23.         RIGHT = 0
  24.     }
  25.  
  26.     public static Quaternion FaceObject(Vector2 startingPosition, Vector2 targetPosition, FacingDirection facing)
  27.     {
  28.         Vector2 direction = targetPosition - startingPosition;
  29.         float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
  30.         angle -= (float)facing;
  31.         return Quaternion.AngleAxis(angle, Vector3.forward);
  32.     }
  33.  
  34.  
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement