Transform Direction Extractor UseLocal result not expected
See original GitHub issueEnvironment
- Code source: github
- Code version: 18d34c1
Steps to reproduce
Goal: make a up-right camera following HeadsetAlias
- Set up tilia xr environment (unityxr, tracked alias, etc)
- Create a camera named “Record Camera”
- Create a game object “Follow Position”, add component “TransformPositionExtractor”
- Drag HeadsetAlias into Source field, Extracted event calls
Record Camera.transform.position
. We want the Record Camera follow the world space position, so turn off UseLocal. - Create a game object “Follow Rotation”, add component “TransformDirectionExtractor”
- Add the custom script AxesToRotation (see the Append). It takes two Vector3 axes and compute a Quaternion rotation.
- Drag HeadsetAlias into Source field of direction extractor, Extracted event calls to AxesToRotation.Forward and AxesToRotation.EmitRotation(). We want the Record Camera’s world space forward direction, so turn off UseLocal.
- AxesToRotation rotation emitted event calls to
Record Camera.transform.rotation
. We want the upright orientation, so we use the ConstructFrom = Forward and Up vectors. - Add MomentProcess to both FollowPosition and FollowRotation, create a MomentProcessor to execute both of them.
Current behavior
The Record Camera’s position is followed correctly, but it is pointing in world space’s z direction, not following the forward direction of HeadsetAlias.
For Transform Position Extractor, when UseLocal is off, it returns the source transform’s world space position, while UseLocal is on, it returns the source transform’s localPosition
protected override Vector3? ExtractValue()
{
...
return UseLocal ? Source.transform.localPosition : Source.transform.position;
However, the current Transform Direction Extractor, when UseLocal is off, it returns the Vector3.forward (which is independent of any source), while UseLocal is on, it returns the source transform’s world space forward instead of local space forward
protected override Vector3? ExtractValue()
{
...
case AxisDirection.Forward:
return UseLocal ? Source.transform.forward : Vector3.forward;
Expected behavior
The transform direction extractor, when UseLocal is off should return the source transform’s world space direction, and when UseLocal is on should return the source transform’s local space direction .
i.e. return UseLocal ? Source.transform.localRotation * Vector3.forward : Source.transform.forward;
Append
namespace VRToolkitExtras.Data.Type.Transformation
{
using System;
using Malimbe.PropertySerializationAttribute;
using UnityEngine;
using UnityEngine.Events;
public class AxesToRotation : MonoBehaviour
{
[Serializable]
public class QuaternionUnityEvent : UnityEvent<Quaternion>
{
}
[Serialized]
public Vector3 Forward { get; set; } = Vector3.forward;
[Serialized]
public Vector3 Up { get; set; } = Vector3.up;
[Serialized]
public Vector3 Right { get; set; } = Vector3.right;
public enum ConstructFromType
{
ForwardAndUp,
ForwardAndRight
}
[Serialized]
public ConstructFromType ConstructFrom { get; set; } = ConstructFromType.ForwardAndUp;
public Quaternion Rotation { get; private set; }
public QuaternionUnityEvent RotationEmitted = new QuaternionUnityEvent();
public void EmitRotation()
{
switch (ConstructFrom)
{
default:
case ConstructFromType.ForwardAndUp:
{
Rotation = Quaternion.LookRotation(Forward, Up);
RotationEmitted?.Invoke(Rotation);
}
break;
case ConstructFromType.ForwardAndRight:
{
Vector3 up = Vector3.Cross(Forward, Right);
Rotation = Quaternion.LookRotation(Forward, up);
RotationEmitted?.Invoke(Rotation);
}
break;
}
}
}
}
Issue Analytics
- State:
- Created 3 years ago
- Comments:13 (13 by maintainers)
Top GitHub Comments
I added all of the prefabs in all of the repos to a scene and just searched for
t:TransformDirectionExtractor
😉Right so you’re saying
the
transform.forward
is the actual global forward directionso in this case
Cube (rotation y 180) –Cube1 (rotation y 180)
in fact the world forward of
Cube1
is stillz 1
because whilst you’ve rotated the Cube1 round 180 to point in the other direction, it’s parent is actually rotating it back to pointing in thez 1
directionbut the local forward of
Cube1
is actuallyz -1
because that locally has been rotated 180