- Home /
How to get the forward vector normal to the camera's forward vector regardless of camera pitch
I hope I can explain this properly. Imagine a 3d world, in it is a plane as ground to stand on, and a character standing upright on top of it. Also, a camera, upright, elevated and pitched down to see the character.
I want to move {using Transform.Translate()} the character forward along the plane in reference to the camera's forward, but technically since the camera is pitched or angled downwards, the camera's forward is also at angle, and the character would sink into the ground while moving forward.
How do I get that forward direction vector relative to the direction the camera is facing? I am very lost with the solutions considering the difference between Quaternion and Vector3. Is there a way to convert Quaternions to Vector3 and vice versa? Ill post an image trying to illustrate the idea, and my code.
my code: using UnityEngine; using UnityEngine.InputSystem;
public class Character : MonoBehaviour
{
#region References
Camera cam;
#endregion References
#region Components
#endregion Components
#region Movement
bool canMove = true;
Vector2 inputDirection;
[SerializeField] float moveSpeed = 5.0f;
#endregion Movement
private void Start()
{
cam = Camera.main;
}
void Update()
{
Move(inputDirection);
}
public void OnMove(InputAction.CallbackContext context)
{
inputDirection = context.ReadValue<Vector2>();
}
private void Move(Vector2 direction)
{
if(!canMove) return;
// Sideways Movement
Vector3 moveSideways = cam.transform.right * direction.x;
//Forward Movement
Quaternion pitchAngle = new Quaternion(cam.transform.rotation.x - Quaternion.identity.x, 0, 0, 0);
Vector3 moveForward = pitchAngle * cam.transform.forward * direction.y;
Vector3 moveVector = moveSideways + moveForward;
transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
}
}
Answer by Hellium · Feb 13 at 12:53 PM
private void Move(Vector2 direction)
{
if(!canMove) return;
Vector3 right = cam.transform.right;
Vector3 forward = Vector3.ProjectOnPlane(cam.transform.forward, Vector3.up).normalized;
Vector3 moveVector = forward * direction.y + right * direction.x;
transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
}
OR
private void Move(Vector2 direction)
{
if(!canMove) return;
Vector3 right = cam.transform.right * direction.x;
Vector3 forward = Vector3.Cross(cam.transform.right, Vector3.up);
Vector3 moveVector = forward * direction.y + right * direction.x;
transform.Translate(moveVector * moveSpeed * Time.deltaTime, Space.World);
}
Note: won't work if the camera is aligned with the world's Y axis.
it works at intended! I wish I could learn how to compute it with Quaternions and Vectors, but ill take it!
Your answer
Follow this Question
Related Questions
Translate Point By Vector3 And Angle 1 Answer
Rotate object instead of camera around it? 0 Answers
Project an object from a perspective camera at same screen position but different Y world position 0 Answers
Unity camera troubles 1 Answer
How could I make my camera move around a point during runtime? 1 Answer