Reputation:
I need a function that calculates new OffSet position that takes 2 arguments old offset and rotation value (the rotation is only a float because the rotation is only happening around 1 axis always. I managed to find how to calculate facing direction based on rotation but I am having problem with offset. Found same topics like this but no answer except making child objects, which is not a good way. So a code like this
var offset = new Vector3(0f,5f,10f);
var rotationY = 90f;
var newOffset = NeededFunction(offset, rotationY);
The newOffset should be a Vector3 (10f,5f,0f). This is the funtion to calculate the facing direction
private Vector3 GetFacingDirection()
{
var rotY = transform.eulerAngles.y;
var vector = Quaternion.AngleAxis(rotY, Vector3.up)
* Vector3.forward;
return vector;
}
Upvotes: 1
Views: 859
Reputation: 1009
Quaternion.LookRotation
of the local position of the child game object. This is childStartRot
. (Calculate in start, or only when you change it).magnitude
property of the child's local position. This is childMag
(Calculate in start, or only when you change it)combinedRot
. (This updates every frame)Vector3.forward * combinedRot
. This is pointOnRot
pointOnRot
. Then multiply pointOnRot
by childMag
to get the offset position.pointOnRot
+ the parent object's position.Example script:
public Quaternion parentRot; //change whenever in play mode
public Vector3 parentPos; //change whenever in play mode
public Vector3 childPos;
Quaternion childStartRot;
float childMag;
void Start()
{
Vector3 local = parentPos - childPos;
childStartRot = Quaternion.LookRotation(local);
childMag = local.magnitude;
}
void Update()
{
Quaternion combinedRot = parentRot * childStartRot;
Vector3 pointOnRot = Vector3.forward * combinedRot;
pointOnRot = pointOnRot.normalized * childMag;
Vector3 output = pointOnRot + parentPos;
//set child object position to output.
}
Upvotes: 0