- Home /
Point Object Towards X/Z Position
Here's an image that kind of shows what I'm trying to do:
http://i.imgur.com/36VZzkf.png
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.
If I understand correctly, all you need to so is zero out the 'y' value of 'dir' before you do the LookRotation(). Insert this between lines 4 and 5:
dir.y = 0.0f;
I think you understand what I'm trying to do--I get what you're suggesting as well.
Now I seem unable to rotate the object whatsoever. Restricting the rigidbody rotation should still allow you to rotate the transform in the code, no?
Yes, you should still be able to rotate the object directly.
I've been checking values, the "lookRotation" in my code always equates to (.0,.7,.0,.7). $$anonymous$$aybe the rotation is too small?
Answer by aldonaletto · Jun 30, 2013 at 07:09 PM
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.
I think I'll opt for a more "puck" like train so that it rolls through the track a bit more naturally and physics doesn't make it hate the corners. I just want to change the direction so that, should we make a real model for the train, it's pointed forward properly.
Your answer
Follow this Question
Related Questions
Finding A Rigidbody's Rotation Speed And Direction 2 Answers
Player character X rotation going haywire on game start 1 Answer
Rotating Rigidbodies 2 Answers