- Home /
Move Character in Relative Direction to Camera
Hi guys, im struggling with moving the character in relation to which way the camera is facing. So what's happening is the world has a North, East, South and East, (+ & - Z and X) and when I press 'W' to move forward the character will always move 'North' no matter the rotation of the camera, how do i make it so my character will always go in the direction of the camera when 'W' is Pressed?
This is my Movement piece:
public void moveForward()
{
Vector3 temp = _XForm_Parent.position;
if (isRunning)
{
temp.z += movementSpeed * runningSpeed;
}
else
{
temp.z += movementSpeed;
}
_XForm_Parent.position = temp;
}
And this is my Orbiting piece:
void LateUpdate()
{
if (!CameraDisabled)
{
if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
{
_LocalRotation.x += Input.GetAxis("Mouse X") * MouseSensitivity;
_LocalRotation.y -= Input.GetAxis("Mouse Y") * MouseSensitivity;
}
_LocalRotation.y = Mathf.Clamp(_LocalRotation.y, 5f, 90f);
if (CanScroll && Input.GetAxis("Mouse ScrollWheel") != 0f)
{
float ScrollAmount = Input.GetAxis("Mouse ScrollWheel") * ScrollSensitivity;
ScrollAmount *= (_CameraDistance * 0.3f);
_CameraDistance += ScrollAmount * -1f;
_CameraDistance = Mathf.Clamp(_CameraDistance, 1.5f, 100f);
}
}
if (Input.GetKey(KeyCode.Mouse3))
{
if (Input.GetAxis("Mouse X") != 0 || Input.GetAxis("Mouse Y") != 0)
{
_LocalRotation.x += Input.GetAxis("Mouse X") * MouseSensitivity;
_LocalRotation.y -= Input.GetAxis("Mouse Y") * MouseSensitivity;
}
_LocalRotation.y = Mathf.Clamp(_LocalRotation.y, 5f, 90f);
if (CanScroll && Input.GetAxis("Mouse ScrollWheel") != 0f)
{
float ScrollAmount = Input.GetAxis("Mouse ScrollWheel") * ScrollSensitivity;
ScrollAmount *= (_CameraDistance * 0.3f);
_CameraDistance += ScrollAmount * -1f;
_CameraDistance = Mathf.Clamp(_CameraDistance, 1.5f, 100f);
}
}
Quaternion QT = Quaternion.Euler(_LocalRotation.y, _LocalRotation.x, 0);
_XForm_Parent.rotation = Quaternion.Lerp(_XForm_Parent.rotation, QT, Time.deltaTime * OrbitDampening);
if (_XForm_Camera.localPosition.z != _CameraDistance * -1f) ;
{
_XForm_Camera.localPosition = new Vector3(0f, 0f, Mathf.Lerp(_XForm_Camera.localPosition.z, _CameraDistance * -1f, Time.deltaTime * ScrollDampening));
}
}
My main question is how do I always make the character move 'Forward' not North, but in relation as to the cameras direction.
Answer by FuryFight3r · Jan 27, 2019 at 11:10 AM
I found the fix, whilst experimenting earlier, I hadn't put .parent. in to move the camera pivot not the camera.
I changed this:
public void moveForward()
{
Vector3 temp = _XForm_Parent.position;
if (isRunning)
{
temp.z += movementSpeed * runningSpeed;
}
else
{
temp.z += movementSpeed;
}
_XForm_Parent.position = temp;
}
To this:
public void moveForward()
{
Vector3 temp = _XForm_Parent.position;
if (isRunning)
{
temp += transform.parent.forward * Time.deltaTime * runningSpeed;
temp.y = 1;
}
else
{
temp += transform.parent.forward * Time.deltaTime * movementSpeed;
temp.y = 1;
}
_XForm_Parent.position = temp;
}
and did the same for left right and backward accordingly.
Answer by gunboldb · Jan 27, 2019 at 07:40 AM
I am just taking a guess here. You could take the appropriate rotation angle of the camera and equal it to the rotation of your character or something to that nature.
Hi thanks for your answer, i did experiment with this, but the movement is being added and subtracted with a vector 3, meaning it only goes by world space, not transform space, i still have this feature implemented, as the object that is rotating to move the camera by mouse, is the same object being moved by the vector 3 with W,A,S,D, how can i convert it to go forward to the transforms position not world position?
Your answer
Follow this Question
Related Questions
Move character and rotate camera on same joystick 0 Answers
Move character in direction its facing 1 Answer
Camera rotate - point and click game 1 Answer
Can the Camera Smoothly rotate AFTER a button is pressed? (Javascript) 1 Answer
Camera RotateAround not rotating around center like a planet 0 Answers