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.
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.
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.