Hey guys,
So im trying to make a top down game where you click to move, everything is working besides the player doesn’t face the direction its moving properly. Sometimes it just moves backwards towards the place clicked, i was wondering if anyone can see what ive done wrong here, any help is appreciated.
using UnityEngine;
using System.Collections;
public class movement : MonoBehaviour
{
public Ray ray;
public GameObject player;
public Vector3 newPos;
RaycastHit hit;
Vector3 moveDir;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
ray = Camera.mainCamera.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit))
{
if (Input.GetMouseButton (0))
{
newPos = new Vector3 (hit.point.x, 2.2f, hit.point.z);
player.transform.position = Vector3.Lerp (player.transform.position, newPos, Time.deltaTime);
moveDir = newPos;
moveDir = new Vector3 (newPos.x, 0.0f, newPos.z);
player.transform.rotation = Quaternion.LookRotation(moveDir * rotationSpeed);
}
}
}
}