Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class LookAtMouse : MonoBehaviour {
- [Tooltip("The main camera used")]
- public Camera mainCamera;
- // keep track of the mouse position
- private Vector3 mousePosition;
- // keep track of where our mouse is in 2d space
- private Vector3 mouseTarget;
- // Use this for initialization
- void Start () {}
- // Update is called once per frame
- void Update () {
- // grab the mouse input
- mousePosition = Input.mousePosition;
- // figure out where we need to rotate towards
- mouseTarget = mainCamera.ScreenToWorldPoint(mousePosition);
- // rotate our barrel towards the mouse
- transform.rotation = Quaternion.LookRotation(Vector3.forward, mouseTarget - transform.position);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement