dabing1022 / Blog

BLOG MARKDOWN BAK AND SOME EXERCISES
http://dabing1022.github.io
85 stars 23 forks source link

Unity3D Notes #11

Open dabing1022 opened 8 years ago

dabing1022 commented 8 years ago
1.Look towards player
using UnityEngine;
using System.Collections;

public class LookTowardMouse : MonoBehaviour {

     void Update () 
     {
         //Mouse Position in the world. It's important to give it some distance from the camera. 
         //If the screen point is calculated right from the exact position of the camera, then it will
         //just return the exact same position as the camera, which is no good.
         Vector3 mouseWorldPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition + Vector3.forward * 10f);

         //Angle between mouse and this object
         float angle = AngleBetweenPoints(transform.position, mouseWorldPosition);

         //Ta daa
         transform.rotation =  Quaternion.Euler (new Vector3(0f,0f,angle));
     }

     float AngleBetweenPoints(Vector2 a, Vector2 b) {
         return Mathf.Atan2(a.y - b.y, a.x - b.x) * Mathf.Rad2Deg;
     }  
 }
dabing1022 commented 8 years ago