var target : Transform;
var last : Transform;
var accel = 2;
static var lapsdone = 0;
static var percent = 0.0;
static var place = 0.0;
function FixedUpdate () {
if (Physics.Raycast (transform.position, Vector3.down, 1.5)) {
rigidbody.AddForce (Vector3.up * 7);
}
if (target.collider) {
var targetPoint = target.position;
var targetRotation = Quaternion.LookRotation (targetPoint - transform.position, Vector3.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * 2.0);
var look = targetPoint - transform.position;
look.y = 0;
targetRotation = Quaternion.LookRotation (look, Vector3.up);
}
rigidbody.AddRelativeForce(Vector3.forward * accel);
var d1 = Vector3.Distance(transform.position, last.position);
var d2 = Vector3.Distance(transform.position, target.position);
var lerpVal = (d1/(d1+d2));
percent = Mathf.Lerp(last.GetComponent(Waypoints).progress, target.GetComponent(Waypoints).progress, lerpVal);
}
This is the code I use for RaceCraft’s AI. The only problem is that the enemies now rotate on all axis to see the waypoints, but some of the waypoints centers are high in the air, causing the enemies to literally fly through the air to reach them. How would I go about limiting them to one axis?
If you want to keep the height at a fixed point you can save your transforms y-coordinate before calculating the new position and restore it’s value after the transformations.
No, the tracks often vary in hight level. I also tried Transform.LookAt, which the documentation says is limited to y unless you add WorldUp, but it was lying! :o
That’s because I misread the Quaternion.SetLookRotation() docs. I’m not quite sure how to do it with the other functions, but I’ll think about it and get back to you.