I'm trying to get an object to smoothly rotate towards a clicked point in my terrain.
However, my code only yields jittery movements in the other direction.
Any help will be greatly appreciated.
using UnityEngine;
using System.Collections;
public class Movement : MonoBehaviour {
public float rotationSpeed = 5.0f;
private Quaternion _lookRotation;
private Vector3 _direction;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
RaycastHit hit;
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
if (hit.collider.tag == "Ground")
{
Vector3 looker = new Vector3(hit.point.x, hit.point.y, 0); //get x and y point from ray cast.
_direction = (looker - transform.position).normalized;
_lookRotation = Quaternion.LookRotation(_direction);
transform.rotation = Quaternion.Slerp(transform.rotation, _lookRotation, Time.deltaTime * rotationSpeed);
}
}
}
}
}