Point Object Towards X/Z Position

Here’s an image that kind of shows what I’m trying to do:

I have a cube riding along a track and I’m trying to make it use physics to do so. My cube slides along just fine so long as the track is straight, I’ve got the physics working basically just right.

What I need to do is point the cube properly so that it doesn’t collide with the edges of the track in a destructive manner. Currently I’ve constrained X/Y/Z rotation on my rigidbody.

		Vector3 ntp = curveHandler.nextTrackPoint(transform.position); // Next track point...
		Vector3 dir = (ntp - transform.forward).normalized;
		if(rigidbody.velocity.magnitude < 3f)
			rigidbody.AddForce(dir * rotationSpeed);
		Quaternion lookRotation = Quaternion.LookRotation(dir);
		transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * rotationSpeed);

This code is what I’m working with.

nextTrackPoint is a method that finds the points the black lines are coming out of in the image. Those points technically lie inside the track however. This method is working just as I intend it to, I can modify that if need be though.

Essentially I want my cube to look at the point, but above the track. That is, I only want it to rotate around its Y axis so that it points at what amounts to just the X/Z coordinate of the black line in the image.

My code isn’t really doing the trick, any advice or direction would be great. This seems like a straightforward fix but I just can’t nail it.

The direction the rigidbody is pointing doesn’t affect (or is affected by) the direction it moves - you must rotate the cube to the movement direction, as you’re currently doing. From your code, I suppose that rotationSpeed is too small: the object tries to follow the track direction, but too slowly to produce the desired effect - values in the range 5 to 20 should work fine.

Besides this, you probably will have problems trying to move the rigidbody along the track with physics: the force adds velocity in the desired direction, but the momentum makes the movement go in an intermediate direction, producing an overshoot. You can reduce the overshoot by using a high drag value: this reduces the momentum and makes the force effect more dominant in the final result.