x


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.

alt text alt text

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.

more ▼

asked Jan 11 '11 at 06:05 PM

paco morales gravatar image

paco morales
119 6 9 24

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.

Jan 11 '11 at 06:21 PM Paulius Liekis

Love the pictures.

Jan 11 '11 at 07:24 PM Statement ♦♦
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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?

  1. 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.
  2. 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;
more ▼

answered Jan 11 '11 at 06:20 PM

Statement gravatar image

Statement ♦♦
20.1k 35 70 175

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

http://forum.unity3d.com/threads/73454-I-can%92t-make-the-Arm-Base-rotate-in-Axis-quot-Y-quot?p=470412#post470412

Jan 11 '11 at 06:45 PM paco morales

I have updated my answer. Basically you are overwriting the world rotation every frame. You should set the local rotation instead.

Jan 11 '11 at 07:23 PM Statement ♦♦

Thanks Man it works !!! I owe you a bottle of Tequila :)

Jan 11 '11 at 08:23 PM paco morales

Where can I collect it? :D

Jan 11 '11 at 08:47 PM Statement ♦♦

You can collect it here in Mexico 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 Mexico :)

Jan 13 '11 at 09:18 PM paco morales
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x2155
x720
x321
x183
x124

asked: Jan 11 '11 at 06:05 PM

Seen: 1915 times

Last Updated: Jan 11 '11 at 07:02 PM