I’m trying to work out a way to do third person movement where the player always moves relative to the direction of the camera. Forward is always the direction the camera is facing, etc. The camera is always centered on the player, which I implemented by having the camera parented to a rigidbody object that is always in the same position as the player; the mouse movement rotates the rigidbody and thus the camera. This part of the code works completely fine.
Camera code
using UnityEngine;
using System.Collections;
public class CameraStuff : MonoBehaviour {
float MouseMovementX;
float MouseMovementY;
float MouseMovementWheel;
public float CameraPanSpeed = 10.0f;
public float CameraScollSpeed = 10.0f;
Vector3 CameraPan;
Vector3 CameraScroll;
Rigidbody CameraFocusPoint;
public Rigidbody Player;
public GameObject PlayerCamera;
Quaternion NewCameraRotation;
void Awake () {
CameraFocusPoint = GetComponent<Rigidbody>();
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
void Update () {
//Get inputs
MouseMovementX = Input.GetAxis("Mouse X");
MouseMovementY = Input.GetAxis("Mouse Y");
MouseMovementWheel = Input.GetAxis("Mouse ScrollWheel");
//Multiply inputs by values set in the inspector and assign them to the desired axes of the object to be controlled
CameraPan = new Vector3(0, MouseMovementX, MouseMovementY) * CameraPanSpeed;
CameraScroll = new Vector3(MouseMovementWheel, 0, 0) * CameraScollSpeed;
//Create a new rotation equal to the current rotation plus the mouse input
NewCameraRotation = Quaternion.Euler(CameraFocusPoint.rotation.eulerAngles + CameraPan);
//Apply the new rotation
CameraFocusPoint.rotation = NewCameraRotation;
//Move the camera forward and backward relative to the focus point
PlayerCamera.transform.localPosition = PlayerCamera.transform.localPosition + CameraScroll;
//Set focus point to be in the same position as the player
CameraFocusPoint.position = Player.position;
}
}
My trouble is getting the player object to move relative to the camera. Every last thing I’ve tried has resulted in the player moving along the global X and Z axes. I’ve tried several ways of setting the player’s rotation to be the same as the camera focus point. The rotation works fine, but every way I’ve tried to apply movement has just moved the player along the global axes anyway. I’ve tried transform.Translate; Rigidbody.movePosition; AddForce (which did nothing, even though the player has a non-kinematic rigidbody); and manually setting the transform.position or the Rigidbody.position. I can’t figure out why nothing I’ve tried applies the movement to the object’s local axes.
This is the movement code as it currently stands.
using UnityEngine;
using System.Collections;
public class PlayerMovement : MonoBehaviour {
float MoveX;
float MoveY;
public float PlayerMovementSpeed = 6.0f;
Rigidbody Player;
public Rigidbody CameraFocus;
float PlayerMotionX;
float PlayerMotionY;
Vector3 AmountToMove;
void Awake() {
Player = GetComponent<Rigidbody>();
}
void Update() {
MoveX = Input.GetAxis("Horizontal");
MoveY = Input.GetAxis("Vertical");
PlayerMotionX = MoveX * 0.5f * PlayerMovementSpeed * Time.deltaTime;
PlayerMotionY = MoveY * PlayerMovementSpeed * Time.deltaTime;
AmountToMove = new Vector3(PlayerMotionX, 0, PlayerMotionY);
Player.position = (Player.position + AmountToMove);
}
}
I have no idea what I’m doing wrong, if anything, or if my approach here is just flawed. It’s not been very good for my sanity. I’d really appreciate any help fixing it or ideas about doing it differently.