C# Playermovement and mouselook

– Add a player with a camera that can look around

– Drag it into the centre of our scene

– Add a character controller component

– Turn on gizmos 

– Create some graphics so that we can easily see it within our scene

– Remove the capsule collider 

– Drag the main camera under our first person player

– Create a PlayerLook script that is of course responsible for our ability to look around

– On the main camera, we want to add a new component and call it MouseLook.

– On void Update, gather some input based on our mouse movement. Create a float called mouseX and set this equal to Input.GetAxis. And the axis that we want to get is the mouse x-axis. This is going to change based on our mouse movement

float mouseX = Input.GetAxis(“Mouse X”);

– We’ll also do the same for the y-axis

float mouseY = Input.GetAxis(“Mouse Y”);

– Create a public float called mouseSensitivity and let’s just default it to 100. Then when we gather the input, we can simply multiply with our mouseSensitivity and because we’re doing this inside the update function, 

– We want to make sure that we rotate independent of our current frame rate. We also want to multiply with Time.deltaTime. 

float mouseX = Input.GetAxis(“Mouse X”) * mouseSensitivity * Time.deltaTime;

– Do the exact same thing to mouse Y

float mouseY = Input.GetAxis(“Mouse Y”) * mouseSensitivity * Time.deltaTime;

– Create a public transform and call it playerBody

– Inside of our update method, we can now access playerBody.Rotate. Here we can specify an axis that we want to rotate around. So we’re going to rotate around Vector3.up and multiply that based on our mouseX

– Implement our mouseY. Create a private variable. Make a float call it xRotation and set this to zero.

– On the update, we can say xRotation minus equals our mouseY 

– Put transform.localRotation equals Quaternion dot Euler. Inside the rotation, we want to put in our xRotation for the x, zero on the y, and zero on the z 

– Add clamping this rotation. We’ll set xRotation equal to Math.Clamp. Inside the clamp, we’ll put our xRotation, negative 90 degrees and 90 degrees.

xRotation = Mathf.Clamp(xRotation, -90f, 90f);

This way we make sure that we can never over-rotate and look behind the player. 

-Add a single line of code in the start function 

-Cursor.lockState = CursorLockMode.Locked;

– Select First person player and add a new component of PlayerMovement script 

– Add some input. Create a float called x and set it equal to Input.GetAxis of horizontal. Same thing with the z and set it equal to Input.GetAxis vertical. 

float x = Input.GetAxis(“Horizontal”);

float z = Input.GetAxis(“Vertical”);

– Create a Vector3 called move. We can simply consider this an arrow that points in the direction that we want to move. So we’ll set this equal to 

Vector3 move = transform.right * x + transform.forward * z;

– We need a reference to our character controller. Create a public CharacterController and call it controller 

public CharacterController controller;

– Below the Vector3, call controller.Move, which is a function on this character controller, inside the move, we can simply put in our move Vector3.

– Create a public float called speed and set it equal to 12 by default. Then down here where we move our player, we can simply multiply our move motion by our speed. Make sure to multiple with Time.deltaTime. This way we make it frame rate independence

Public float speed = 12f;

controller .Move(move * speed * Time.deltaTime); 

– Drag Character controller to player movement’s controller

– On top of PlayerMovement’s script, create a Vector3, this is going to store our current velocity 

– Inside the method, increase velocity.y

public float gravity = -9.81;

velocity.y += gravity * Time.deltaTime;

controller.Move(velocity * Time.deltaTime);

– For groundCheck, create a public transform called groundCheck and also create a public float with the groundDistance, and finally create a public layer mask and call this ground mask 

public Transform groundCheck;

public float groundDistance = 0.4f;

Public LayerMask groundMask;

– We’ll create private variable. This is going to be bool of isGrounded. 

– Inside of our update function at the very top, set isGrounded equal to the result of our physics check. Physics.CheckSphere. This is going to create a sphere based on the groundCheck.position. Since we’re not moving down, we’re standing on the ground. 

isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);

if(isGrounded && velocity.y < 0)

{

Velocity.y = -2f;

}

– In our ground mask, add a layer of ground

– Inside the script of PlayerMovement, create an Input.GetButtonDown, jump. This is another default input that automatically maps to the space key and we’re currently grounded. Set velocity.y equal to the square root, so Math.Sqrt. Create a public float with the jumpHeight

Public float jumpHeight = 3f; 

if(Input.GetButtonDown(“Jump” ) && isGrounded)

{

Velocity.y = Mathf..Sqrt(jumpHeight * -2f * gravity);

}