- Home /
How to fix the rotation and Orientation in an object as a Children ?
Hello,
I'm working in this Robot-Toy, I am trying to fix the rotation of the arm, but the Arm rotates keeping the same orientation don't change (always see to the same direction).
(The Base's Arm (in color green) rotates well, but the Arm as a "children" rotates keeping the same orientation)
Please take a look to the image.
Here it is the Script I am using for the base in green color.
var gunSpeedNeg: int = -40; var gunSpeedPos: int = 40; function Update () {
if(Input.GetKey("r")){
transform.Rotate(Vector3.right* gunSpeedNeg* Time.deltaTime);
}
if(Input.GetKey("y")){
transform.Rotate(Vector3.right* gunSpeedPos* Time.deltaTime);
}
}
And here it is the script for the arm " who has to rotate according to the arm but it doesn't (It rotates but always keeping the same orientation)
var gunSpeed : float = 40;
private var rotation : Quaternion = Quaternion.identity; private var left : Quaternion = Quaternion.AngleAxis(-150, Vector3.up); private var right : Quaternion = Quaternion.AngleAxis(30, Vector3.up);
function Start () { // This piece of code will allow you to remember the origin. rotation = transform.rotation; left = rotation left; right = rotation right; }
function Update () { var speed = gunSpeed * Time.deltaTime;
if(Input.GetKey("a"))
{
rotation = Quaternion.RotateTowards(rotation, left, speed);
}
if(Input.GetKey("d"))
{
rotation = Quaternion.RotateTowards(rotation, right, speed);
}
transform.rotation = rotation;
}
Any advice is welcome.
Could you rephrase your question? It's very unclear which parts you want to rotate and which you want to keep static. Because basically if you parent some thing and then rotate parent, then children will rotate with parent.
Answer by Statement · Jan 11, 2011 at 06:20 PM
Your code should rotate the whole hierarchy. It sounds to me that your children have some code affecting their world rotation somehow. Are there other scripts involved?
- Make sure your hierarchy is nested, not flat. You might have written code that set position to some joint manually, which would explain the behavior.
- Check all other scripts that can influence the rotation or position of the child parts to isolate the issue.
In your updated code you set rotation this way:
transform.rotation = rotation;
This sets the world rotation (absolute rotation) of the object. You likely want to set the local rotation, so hierarchy rotations are preserved. This is done simply.
transform.localRotation = rotation;
Likewise, you should store the localRotation in your Start function.
rotation = transform.localRotation;
Hi Statement,
Yes you help me to make the scripts for the arm, not for the base. here you can see the scripts, but let me rephrase my question for Paulius Liekis.
Please take a look to my thread
I have updated my answer. Basically you are overwriting the world rotation every frame. You should set the local rotation ins$$anonymous$$d.
Thanks $$anonymous$$an it works !!! I owe you a bottle of Tequila :)
You can collect it here in $$anonymous$$exico or I can pay you the cost of it, But I don't know about prices in Sweden I think is a little expensive than $$anonymous$$exico :)
Your answer
Follow this Question
Related Questions
how to know if an axis has been full rotated? 1 Answer
How can i rotate the aircraft elevator around the yellow axis up and down ?? as shown in the pic 0 Answers
Rotating an object on Z axis while moving 1 Answer
Moving an object based on the position of another object 2 Answers
Need help with rotation script. 1 Answer