Reputation: 451
I'm trying to set a GameObjects position relative to another GameObjects local position, but I'm not having alot of luck. With a raycast I check on which side the player stands, which is all working fine and I'm trying to create a transform relative to the localposition of the object depending on which side you stand on. When this object is not rotated it all works fine, but when I rotate the object the target positions seem to still be in worldspace. I have added images to illustrate the case.
Album with images illustrating the problem
if(right)
{
Vector3 Pos1 = _pushableT.localPosition;
Pos1.x = _pushableT.localPosition.x + distBetween;
_targetPos = Pos1;
}
if (left)
{
Vector3 Pos2 = _pushableT.localPosition;
Pos2.x = _pushableT.localPosition.x - distBetween;
_targetPos = Pos2;
}
if (front)
{
Vector3 Pos3 = _pushableT.localPosition;
Pos3.z = _pushableT.localPosition.z + distBetween;
_targetPos = Pos3;
}
if (back)
{
Vector3 Pos4 = _pushableT.localPosition;
Pos4.z = _pushableT.localPosition.z - distBetween;
_targetPos = Pos4;
}
targetBallDebug.position = _targetPos;
Upvotes: 0
Views: 49
Reputation: 51
This will work fine for position of object, that looks at target, but not ideal for rotation in 3d space
using UnityEngine;
public class TrackToTarget : MonoBehaviour
{
[SerializeField]
private Transform target;
private Vector3 releativePosition;
private Quaternion targetRotationAtStartInv;
private Quaternion rotationDiffernce;
private void Start()
{
releativePosition = target.position - transform.position;
targetRotationAtStartInv = Quaternion.Inverse(target.rotation);
rotationDiffernce = target.rotation * transform.rotation;
}
private void Update()
{
transform.position = target.position - target.rotation * (targetRotationAtStartInv * releativePosition);
transform.rotation = rotationDiffernce * target.rotation;
}
}
Upvotes: 1