Home > Enterprise >  Weird results with Transform.TransformDirection()
Weird results with Transform.TransformDirection()

Time:03-13

I'm using portal.TransformDirection() to simulate how the object the code is attached to would rotate if it were a child of the portal object without actually being its child.

This is the code and it works kind of but not really. Only if I rotate the portal Object around the Y axis. It still does something when I rotate around the other axes but not what I'm expecting.

I didn't find anyone else who had this problem so I thought I might ask.

I have a short video that demonstrates how it rotates (https://youtu.be/dfvr4IrA2SU) and here is the code on the object that should rotate with the portal object:

using UnityEngine;

[ExecuteInEditMode]
public class PortalArrow : MonoBehaviour
{
    [SerializeField] Transform portal;
    [SerializeField] Vector3 eulerRotation;

    void LateUpdate()
    {
        transform.position = portal.position;
        transform.eulerAngles = portal.TransformDirection(eulerRotation);
    }
}

The variable eulerRotation is just a constant Vector3. nothing special about it.

CodePudding user response:

eulerAngles are no direction and it makes no sense to treat them like one.

The fact that it is also stored as a Vector3 is a pure convinience.


would rotate if it were a child of the portal object without actually being its child.

It sounds like you rather simply want

transform.rotation = portal.rotation;

CodePudding user response:

Your problem is you're using eulerAngles as if it represents a look direction but it does not, it represents three rotations about z, y and z.

Don't use euler angles, just set the rotation directly via transform.rotation and Quaternion.SetLookRotation.

transform.rotation.SetLookRotation(portal.TransformDirection(eulerRotation));

This will correctly apply the look direction rotation.

  • Related