- Home /
How to get left forward and right forward vector position from player of distance 10 and with angle 15 from player position?
I am new to unity and Vector math. I am trying to calculate the forward left and forward right vector position of distance 10 and angle of 15 degrees from the player position.
A = player position
B = Forward Right
C = Forward Left
how to calculate B and C with every movement of player?
Answer by jackmw94 · Jan 26, 2021 at 10:48 AM
Assuming the player's transform is facing forward, playerTransform.forward
will be the vector stating that forward direction.
This will be a unit vector so its magnitude will be 1, to make it 10 we can multiply it by 10.
Now to rotate it. When rotating things, use quaternions - they sound scary (and kind of are) but as long as you use their functions and ignore what's going on underneath they're pretty sweet. So to rotate your scaled forward vector around the up axis we would write:
var scaledForward = playerTransform.forward * 10f;
var leftRotated = Quaternion.AngleAxis(-15f, playerTransform.up) * scaledForward;
var rightRotated = Quaternion.AngleAxis(15f, playerTransform.up) * scaledForward;
These leftRotated / rightRotated vectors are the offsets from the player's position. To get the actual positions of B and C you can add these to the player's position:
var positionB = playerTransform.position + leftRotated;
var positionC = playerTransform.position + rightRotated;
Hope this helps, feel free to reply here if there's anything you don't understand! :)
Answer by FeedMyKids1 · Jan 26, 2021 at 10:59 AM
[SerializeField] float distForward = 10f;
[SerializeField] float arcAngle = 15f;
private void OnDrawGizmosSelected()
{
Vector3 forward = transform.position + transform.TransformDirection( Vector3.forward * distForward);
Quaternion leftRot = Quaternion.AngleAxis(-arcAngle, transform.up);
Quaternion rightRot = Quaternion.AngleAxis(arcAngle, transform.up);
Vector3 forwardToLeft = leftRot * forward;
Vector3 forwardToRight = rightRot * forward;
Gizmos.color = Color.red;
Gizmos.DrawLine(transform.position, forward);
Gizmos.color = Color.blue;
Gizmos.DrawLine(transform.position, forwardToLeft);
Gizmos.color = Color.green;
Gizmos.DrawLine(transform.position, forwardToRight);
}
Put this on the player to visualize where those points are.
Blue is left, Green is right.
transform.forward may not inherently work, that's why you see the transform.TransformDirection call. It will convert world space to local.
You can adjust the distance forward and the angle in the inspector. Just put this into a script on the player.
Your answer
Follow this Question
Related Questions
Having problem with Vector3.distance 1 Answer
GameObject's transform in Vector3 1 Answer
What is the 3rd point of Vector3.Angle ? 1 Answer
Vector3.Distance is acting erratically 2 Answers
Can't change vector direction 0 Answers