- Home /
Setting rotation Angle / rotation question
Hello,
can I set objects rotation angle (vs rotating it by some degree?) say set angle 90, (on local axis)
long version
I have Cylinder with 6 sides, some object is moving on a path through this cylinder. this object is at the center of the cylinder all times, another object is attached to it and is some distance away from this object.
sorry for the crappy pic (: did it in paint
I want to rotate it left 60 degrees when A is hit, and rotate right 60 degrees when I hit D. basically its like sides of cylinder are racing tracks, and with each rotation player is moved(rotated) to the center of next side.
I'm using this script
var rotation = 0;
function Update () { //rotate player when Ais hit if (Input.GetKeyDown ("a")) { // rotation += 0.2; transform.Rotate (rotation,0,0); } if (Input.GetKeyDown ("d")) { // rotation += 0.2 transform.Rotate (rotation*-1,0,0); } }
and I get some weird result like 29.9, 30.1 rotations when default rotation of null is 90 O_O so I figured if I have array with each possible angle in it, and I directly set angle of object to some degrees I will avoid this weird angles. but how do I do that?
there's a code button when you're editing (it has 010101 on it), which just indents the text by 4 spaces. That's all that is needed to format code.
Answer by duck · Apr 21, 2010 at 09:16 AM
How are you moving your null object along the centre line of the tube?
Are you also aligning your object with the direction of the tube's path?
What you should be doing, each Update() is this:
1) Set your null object to the new position along the path
2) Set your null object's rotation so that it is aligned with the path direction. For example:
nullObject.transform.rotation = Quaternion.LookRotation( pathDirection );
3) Now rotate the null object around its own local forward axis by the full amount (because the previous command will have reset the rotation entirely):
nullObject.Rotate(Vector3.forward * playerAngle);
So, when put together, your Update() function might look something like this:
float playerAngle = 0; float turnSpeed = 30;
void Update() {
playerAngle += Input.GetAxis("Horizontal") * turnSpeed * Time.deltaTime;
transform.position = GetPathPosition();
Vector3 pathDirection = GetPathDirection();
nullObject.transform.rotation = Quaternion.LookRotation( pathDirection );
nullObject.transform.Rotate(Vector3.forward * playerAngle);
}
thx for the reply. I'm using Spline Controller for path http://www.unifycommunity.com/wiki/index.php?title=Spline_Controller and it does not set objects rotation, just position(if i'm not mistaken) http://j.imagehost.org/0316/untitled_2.png (direction is same as on horizontal, even though path curves)
I don't really know how paths work, but i will try to figure it out
To get the current direction of the route, just get the position of another point slightly further along the spline, and do: (furtherPosition-currentPosition). This gives you a vector whose direction is the direction of the spline at that point.
Your answer
Follow this Question
Related Questions
Rotate on one axis and check if angle is inside desired range 2 Answers
Set rotation based on 1,1,1 style vector? How to convert vector3 to quaternion? 1 Answer
How to Find an Object close to where the Player is Looking 1 Answer
Calculating the corners of a GameObject (sprite) with Quaternion.Angle 2 Answers
Problem with angles and rotations in a 2D top down space shooter 0 Answers