- Home /
Trying to rotate my catapult's launch arm.
This is similar code to my last problem but the problem itself seems to be different.
As part of my catapult, there is an 'arm' that holds the projectile. The player is able to raise or lower this arm around its pivot, affecting the velocity of the projectile object upon launch.
The catapult arm is a gameObject holding two things: the visible catapult arm with mesh collider. another arm with no mesh renderer or collider, placed opposite the other arm, its purpose being to create the centre or rotation for the encapsulating gameObject.
Now, when the player presses up, the arm should raise unless it is already at 90 degrees to the base of the catapult frame. Also, when down is pressed, the arm should lower unless it is already at 10 degrees to the base.
The arm moves fluently, applying help I had previously from aldonaletto.
The problem I am facing is that for some reason when testing the arm seems to rotate in a full circle without restraint, only slowing down when it seems to be almost 180 degrees from its original position. The x rotation seems to peek around 90 and then as the arm moves through the catapult frame it carries on and the x rotation appears to decrease. It doesn't stop where I need it to.
My code is as follows:
gameObject.tag == "catapult arm";
function Update() { if(transform.Rotation.x < 90 && transform.Rotation.x > 10) { if(Input.GetKey ("up")) { transform.Rotation.x -= 0.01; } if(Input.GetKey ("down")) { transform.Rotation.x += 0.01; } } }
i might be very wrong here, but are you sure, that transform.Rotation.x is giving you a return value in full degrees?
From the Script Reference:
"The rotation of the transform in world space stored as a Quaternion. Unity stores rotations as Quaternions internally. "
I really have no idea. The value I was referring to when testing is from the inspector Transform box with the catapult arm selected. I believe these are degrees.
$$anonymous$$aybe try to Debug this with:
Debug.Log(transform.Rotation.x); or print(transform.Rotation.x);
Before and After the If Statement.
Answer by syclamoth · Jan 04, 2012 at 05:28 PM
The number from 'rotation.x' and the 'x' component in the transform box are not the same. It's important to understand this- the three numbers in the transform box are a Euler Angle representation of the rotation!
If you use rotation.euler.x instead, it will give you a more correct number.
In this case, however, you should just use Transform.Rotate, since it manages all of that for you.
How do I incorporate Transform.Rotate?
Am I to replace the transform.rotation.x? The intellisense does not show Rotate as a member of Transform...
I changed the transform.rotation.x to transform.rotation.eulerAngles.x and it works perfectly. Thankyou for your help. :)
Oh, sorry, I wan't being clear. Rotate is a function of the object, not the class. Look it up in the documentation- it's there to help you! In fact, if everyone read the documentation before posting here there probably wouldn't be half as many questions.
tl;dr, use it like this-
transform.Rotate(some amount);
this makes the Transform 'transform' rotate.
thanks for the update, however your other advice of using eulerAngles was sound :) another thumbs up for you though
Your answer
Follow this Question
Related Questions
physics & rotation question 1 Answer
Keep rotation when changing parent in Unity 3D 1 Answer
Moving forward in world space with rotation. 1 Answer
Turret Limitations on a Moving Object 0 Answers
limiting rotation to solid angle 5 Answers