vector3 rotatetowards don't work
Hello,
i have created this script but this don't rotate the object to the walking point. whats wrong?
using UnityEngine; using System.Collections;
public class PathFollower : MonoBehaviour {
public Transform[] path;
public float speed = 5.0f;
public float reachDist = 1.0f;
public int currentPoint = 0;
void Start(){
}
void Update(){
float dist = Vector3.Distance (path [currentPoint].position, transform.position);
transform.position = Vector3.MoveTowards (transform.position, path[currentPoint].position, Time.deltaTime*speed);
Vector3 newrot = Vector3.RotateTowards (transform.position, path[currentPoint].position, Time.deltaTime*speed, 1.0f);
transform.rotation = Quaternion.LookRotation(newrot);
GetComponent<Animation>().Play("WalkForward");
if (dist <= reachDist) {
currentPoint++;
}
if (currentPoint >= path.Length){
currentPoint = 0;
}
}
}
Comment
Answer by toromano · Oct 19, 2015 at 12:29 PM
LookRotation needs a relative position. You should use it like this:
transform.rotation = Quaternion.LookRotation(path[currentPoint].position - transform.position);
if you want a smooth rotation, you can use this:
float step = speed * Time.deltaTime;
var targetRotation = Quaternion.LookRotation(path[currentPoint].position - transform.position);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation , step);