- Home /
need help with player movement for 3rd person project
hi, really new to unity and coding as a whole. trying to learn the basics so i can put together a simple game, at this point i have a cube as a player that has basic movement and jumping with a bool to ensure no double jumps. iv gotten a camera working which can rotate around the player with joysticks or a mouse as id like this to work with a controller. the camera also has some basic collision but iv noticed that the movement of the player is relative to the world axis not the direction the camera is facing which id prefer eg: forward movement should be whichever direction the camera is facing. to get this far i have combined a few tutorials for each bit of the project. if someone could take a look at my code and help me get the movement working properly i would be very grateful, would love a decent explanation of what it is you did to help so i can learn from this. 2 of my 3 scripts are here, my playermovement and camerafollow, didnt include the collision script as i highly doubt it would be relevant here, if it is let me know and ill add it. Thanks in advance
first script is my camera control script
second is the script for my player controller
public class CameraFollow : MonoBehaviour
{
public float CameraMoveSpeed = 120f;
public GameObject CameraFollowObject;
Vector3 FollowPosition;
public float ClampAngle = 80f;
public float InputSensitivity = 150f;
public GameObject CameraObject;
public GameObject PlayerObject;
public float CameraDistaneXToPlayer;
public float CameraDistaneYToPlayer;
public float CameraDistaneZToPlayer;
public float MouseX;
public float MouseY;
public float FinalInputX;
public float FinalInputZ;
public float SmoothX;
public float SmoothY;
private float RotationY = 0f;
private float RotationX = 0f;
// Start is called before the first frame update
void Start()
{
Vector3 rot = transform.localRotation.eulerAngles;
RotationY = rot.y;
RotationX = rot.x;
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
// Update is called once per frame
void Update()
{
//this sets up the rotation of the sticks/mouse
float InputX = Input.GetAxis("RightStickHorizontal");
float InputZ = Input.GetAxis("RightStickVertical");
MouseX = Input.GetAxis("Mouse X");
MouseY = Input.GetAxis("Mouse Y");
FinalInputX = InputX + MouseX;
FinalInputZ = InputZ + MouseY;
RotationY += FinalInputX * InputSensitivity * Time.deltaTime;
RotationX += FinalInputZ * InputSensitivity * Time.deltaTime;
RotationX = Mathf.Clamp(RotationX, -ClampAngle, ClampAngle);
Quaternion localRotation = Quaternion.Euler(RotationX, RotationY, 0.0f);
transform.rotation = localRotation;
}
private void LateUpdate()
{
CameraUpdater();
}
void CameraUpdater()
{ //sets the target object to follow
Transform target = CameraFollowObject.transform;
//move towards the game object that is the target
float step = CameraMoveSpeed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
public class PlayerMovement: MonoBehaviour
{
public float PlayerSpeed;
public float JumpHeight;
private Rigidbody Player;
private bool IsGrounded;
void Start()
{
Player = GetComponent<Rigidbody>();
}
void Update()
{
Player.velocity = new Vector3(Input.GetAxis("Horizontal") * PlayerSpeed, Player.velocity.y, Input.GetAxis("Vertical") * PlayerSpeed);
if (Input.GetButtonDown("Jump"))
{
if (IsGrounded == true)
Player.velocity = new Vector3(Player.velocity.x, JumpHeight, Player.velocity.z);
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
IsGrounded = true;
Debug.Log("grounded");
}
}
private void OnCollisionExit(Collision collision)
{
if (collision.gameObject.tag == "Ground")
{
IsGrounded = false;
Debug.Log("ungrounded");
}
}
}