Im trying to implement a move to clicked point script in Unity 4.5, I tried to use my previous script, made in Unity 3.5, but it didnt worked. I googled a lot, but all the solutions I found dont work. This is my current version:
void Update () {
if (Input.GetKeyDown(KeyCode.Mouse0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Plane playerPlane = new Plane(Vector3.up, transform.position);
RaycastHit hit;
float hitdist = 0.0F;
//if (Physics.Raycast(ray, out hit, 1000.0f)) {
if (playerPlane.Raycast(ray, out hitdist)) {
Vector3 targetPoint = ray.GetPoint(hitdist);
destination = ray.GetPoint(hitdist);
Quaternion targetRotation = Quaternion.LookRotation(targetPoint - transform.position);
transform.rotation = targetRotation;
}
}
Animation walk = GetComponent<Animation>();
walk.Play("walk");
transform.position = Vector3.MoveTowards(transform.position, destination, Time.deltaTime * 1.5F);
}
The previous script moves the character, but rotates it to horizontal position, face down, and also ignores terrain collider: the character gets into the ground if there is some hill in the path. How can I solve this?