Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.83 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class LookAtMouse : MonoBehaviour {
  6.  
  7.     [Tooltip("The main camera used")]
  8.     public Camera mainCamera;
  9.  
  10.     // keep track of the mouse position
  11.     private Vector3 mousePosition;
  12.     // keep track of where our mouse is in 2d space
  13.     private Vector3 mouseTarget;
  14.  
  15.     // Use this for initialization
  16.     void Start () {}
  17.    
  18.     // Update is called once per frame
  19.     void Update () {
  20.         // grab the mouse input
  21.         mousePosition = Input.mousePosition;
  22.         // figure out where we need to rotate towards
  23.         mouseTarget = mainCamera.ScreenToWorldPoint(mousePosition);
  24.         // rotate our barrel towards the mouse
  25.         transform.rotation = Quaternion.LookRotation(Vector3.forward, mouseTarget - transform.position);
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement