Limiting rotation to Y

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

You misread: worldUp is a hint vector. That is, it will be on the local YZ plane after the function is called.

What you can try is use is:

targetRotation = transform.rotation.SetLookRotation(ZeroY(target.position));

…where ZeroY is:

function ZeroY(vector : Vector3) : Vector3 {
	return new Vector3(vector.x, transform.position.y, vector.z);
}

And do your lerping from there.

Like this?

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.AddRelativeForce (Vector3.forward * 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 = transform.rotation.SetLookRotation(ZeroY(target.position));

	}
	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);
}

function ZeroY(vector : Vector3) : Vector3 {
   return new Vector3(vector.x, transform.position.y, vector.z);
}

That gives me an error on line 18, cannot convert void to UnityEngine.Quaternion

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.

Thank you for your help! n_n