using UnityEngine;
using System.Collections;
public class Mouselock : MonoBehaviour {
//The the selected gameobject
public GameObject targeted;
// how quickly you want to be able to look at the target.
public float speed;
// Use this for initialization
void Start () {
//Fills the array with GameObjects
//uncomment this line if you do not need to check if new objects are being instantiated
//targetable = GameObject.FindWithTag("Targetable");
}
// Update is called once per frame
void Update () {
//creates a ray from the mouse cursor to the camera
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
//detects to see if the ray hits an object with the tag Targetable and if the mouse is pressed down.
Physics.Raycast(ray, out hit);
if(hit.collider.tag == "Targetable" && Input.GetMouseButtonDown(0)){
//selects the target
targeted = hit.collider.gameObject;
}
// lock on target
Quaternion targetRotation = Quaternion.LookRotation (targeted.transform.position - transform.position);
// Smoothly rotate towards the target point.
transform.rotation = Quaternion.Slerp (transform.rotation, targetRotation, speed * Time.deltaTime);
}
}
It looks at the object that I click on but it only looks at the object when the mouse is hovering over it.