#Rotation of connected object.

1 messages · Page 1 of 1 (latest)

civic kraken
#

Hello, I have several objects that I can combine. They have a snap point. Where the origin of the next object is set to the SnapPoint position of the previous object and then assigned as children to this SnapPoint. When I select an object and try to rotate it, the first object goes 15 degrees as it should, but the next one rotates every 30 degrees and the third attached object goes 45 degrees. I tried to tweak something with the code but so far I have no idea. Could someone please help? ```public class ObjectRotator : MonoBehaviour
{
public float rotationAngle = 15f;

private GameObject selectedObject;
private bool isObjectSelected;

void Update()
{
    // Check if the user has selected an object
    if (Input.GetMouseButtonDown(0))
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        if (Physics.Raycast(ray, out hit))
        {
            // Check if the object is not the plane
            if (hit.collider.gameObject.CompareTag("Plane"))
            {
                return;
            }

            selectedObject = hit.collider.gameObject;
            isObjectSelected = true;
        }
    }

    // Check if the user has pressed the A or D keys
    if (isObjectSelected && (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D)))
    {
        // Rotate the selected object around the Y-axis
        float direction = Input.GetKeyDown(KeyCode.A) ? -7.5f : 7.5f;
        selectedObject.transform.rotation *= Quaternion.Euler(0f, direction, 0f);
    }
}

}```

deft patrol
#

Do all these objects parent to each other, but each get an instance of this script?

#

Yes, so this is intended behaviour. Your rotations are indeed in global space, but each time an object rotates so does anything relatively parented to it.

#

Probably, you should be overwriting your rotation instead of applying them via *=.

civic kraken
#

Right I have something like this '''if (boxCollider != null && other.name != "SnapPoint" && other.gameObject.layer != 3)
{
//Debug.Log("Trigger Stays");
other.transform.parent = transform;
other.transform.localPosition = Vector3.zero;
other.transform.localRotation = Quaternion.identity;
boxCollider.enabled = true;

                // Change the layer of the snapped object and its children to layer 3
                other.gameObject.layer = 3;
                foreach (Transform child in other.transform)
                {
                    child.gameObject.layer = 3;
                }
            }'''
#

How can overwirte if you can give me some brief tip

#

I am quite new to this 😄

deft patrol
#
                {
                    //Debug.Log("Trigger Stays");
                    other.transform.parent = transform;
                    other.transform.localPosition = Vector3.zero;
                    other.transform.localRotation = Quaternion.identity;
                    boxCollider.enabled = true;

                    // Change the layer of the snapped object and its children to layer 3
                    other.gameObject.layer = 3;
                    foreach (Transform child in other.transform)
                    {
                        child.gameObject.layer = 3;
                    }
                }'''```
#

I don't see how that new code you posted relates to rotating anything.

civic kraken
#

oh okay I misunderstood you I though that I was supposed to overwrite the position of the snapping. My bad.

deft patrol
#

If you have all these objects chained together in a hierarchy, their rotations are like an elbow that all children hang from.

#

If every object rotates it's elbow a bit, you end up with this.

#

If you want only the top-level element to rotate once, that's the only joint in the chain that needs to rotate.

civic kraken
#

I am trying to do somekind of slide that you can modify it by rotating everypart so if the first one is getting rotated the rest should also. And they do rotate but every origin further from the starting part is going more and more than 15 degrees in Y axis.

deft patrol
#

That first element is parented in space, with everything else being relatively unrotated.

deft patrol
civic kraken
#

Yes

deft patrol
#

Does my second image match what you expect to happen?

civic kraken
#

Sorry if I am confusing

deft patrol
#

This looks to me like what your first case is doing.

civic kraken
#

Yea it seems so.

deft patrol
#

If your intention is for these things to be spiraling off each other and also be parented, then you do probably want an additive rotation. A spiral would incrementally rotate a bit at each node.

#

My understanding here is that if the slide changes relative rotation at different points, then those points each need a different rotation. Do you not want the child elements to rotate when their parent does?

#

Yes, that would happen to the rotated object's children.

civic kraken
#

Yea but the problem is the one that I rotated goes 15 degrees, but If I want to rotate another one the one click of the button is no longer rotating the object by 15 degrees.

#

And that is my main problem

deft patrol
#

Does this improve if you change your rotation line to: selectedObject.transform.Rotate(Quaternion.Euler(0f, direction, 0f), Space.Self);?

civic kraken
#

Its says "cannot convert from 'UnityEngine.Quaternion' to 'UnityEngine.Vector3'"

#

selectedObject.transform.Rotate(Vector3.up, direction, Space.Self); also tried with this but still sometimes it is 15 sometimes it goes 22,5

deft patrol
#

Now if I'm following correctly, the object that you're targeting rotates correctly, but you don't want the children of that object to move at all, is that correct?

civic kraken
#

The first object is rotating by 15 degrees but If I select for example next one it does not rotate by 15 degrees anymore but by 22,5. Where in the script is specified that it should update local rotation of the origin on Y axis by 15 degrees every key click.

deft patrol
#

Is the code that you posted the same that you are using? In what you posted above, public float rotationAngle is never used.

#

This code should be rotating by 7.5 degrees. If you are setting an angle in the inspector, this code will never see it - only change by 7.5 or -7.5 depending on the key pressed.

civic kraken
#

if (isObjectSelected && (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.D)))
{
// Rotate the selected object around the Y-axis
float direction = Input.GetKeyDown(KeyCode.A) ? -rotationAngle /2 : rotationAngle /2;
selectedObject.transform.Rotate(Vector3.up, direction, Space.Self);
}

#

This one is for now

deft patrol
#
        {
            // Rotate the selected object around the Y-axis
            float direction = Input.GetKeyDown(KeyCode.A) ? -rotationAngle /2 : rotationAngle /2;
            selectedObject.transform.Rotate(Vector3.up, direction, Space.Self);
        }```
civic kraken
#

I did specify that is 15f but it was going by 30 so I divided it by 2

#

To be honest rotations in Unity are really confusing.

deft patrol
#

Sometime in the future, it might be a good idea to switch to using quaternions instread of euler angles for algebra concerning rotations.

civic kraken
#

Okay I think that was a issue here

civic kraken
#

Thank you for being patient with me