Write first person fully working controller for Unity
- User
Write first person fully working controller for Unity
- Assistant
Write first person fully working controller for Unity
Sure! I'll help you create a fully functional first-person controller in Unity using C#. This controller will allow your player to move around, look in all directions, and jump. We'll go through setting up the player object, writing the script, and configuring the scene.
a. Create a New Unity Project (if you don't have one):
b. Add a Player Object:
c. Add a Capsule to Represent the Player Body:
d. Add a Character Controller Component:
a. Create a Scripts Folder:
b. Create the Script:
c. Write the Script:
Double-click on the FirstPersonController script to open it in your code editor (Visual Studio, Visual Studio Code, etc.), and enter the following code:
using UnityEngine;
public class FirstPersonController : MonoBehaviour
{
[Header("Movement Settings")]
public float walkSpeed = 5f;
public float runSpeed = 8f;
public float jumpHeight = 2f;
public float gravity = -9.81f;
[Header("Mouse Settings")]
public float mouseSensitivity = 100f;
public bool lockCursor = true;
private CharacterController characterController;
private Vector3 velocity;
private Transform cameraTransform;
private float xRotation = 0f;
void Start()
{
characterController = GetComponent<CharacterController>();
cameraTransform = Camera.main.transform;
if (lockCursor)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
}
void Update()
{
MovePlayer();
MouseLook();
}
void MovePlayer()
{
// Check if the player is grounded
if(characterController.isGrounded && velocity.y < 0)
{
velocity.y = -2f; // Small value to ensure grounded state
}
// Get input for movement
float moveX = Input.GetAxis("Horizontal");
float moveZ = Input.GetAxis("Vertical");
// Determine movement direction
Vector3 move = transform.right * moveX + transform.forward * moveZ;
// Check for running
float speed = Input.GetKey(KeyCode.LeftShift) ? runSpeed : walkSpeed;
// Move the player
characterController.Move(move * speed * Time.deltaTime);
// Jumping
if(Input.GetButtonDown("Jump") && characterController.isGrounded)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
}
// Apply gravity
velocity.y += gravity * Time.deltaTime;
// Move the player based on gravity
characterController.Move(velocity * Time.deltaTime);
}
void MouseLook()
{
// Get mouse input
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity * Time.deltaTime;
// Calculate rotation for looking up and down
xRotation -= mouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
// Apply rotation to the camera
cameraTransform.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
// Rotate the player object to look left and right
transform.Rotate(Vector3.up * mouseX);
}
}
a. Set Up the Camera as a Child of the Player:
b. Position the Camera:
c. Tag the Camera:
a. Save Your Scene:
b. Add a Ground Plane:
c. Play the Game:
Variables and Settings:
walkSpeed: Speed when walking.runSpeed: Speed when running.jumpHeight: Height of the jump.gravity: Gravity value (negative to pull down).mouseSensitivity: Sensitivity of mouse movement.lockCursor: Whether to lock the cursor to the center of the screen.Components:
CharacterController: Unity's built-in component to handle player movement and collisions.cameraTransform: Reference to the player's camera.Functions:
Start(): Initializes variables and locks the cursor if lockCursor is true.Update(): Called every frame to handle movement and mouse look.MovePlayer(): Handles movement input, running, jumping, and gravity.MouseLook(): Handles looking around with the mouse.Key Mechanics:
Input.GetAxis("Horizontal") and Input.GetAxis("Vertical").transform.right, transform.forward).jumpHeight.walkSpeed and runSpeed to change movement speeds.mouseSensitivity for faster or slower looking around.jumpHeight to make the player jump higher or lower.gravity if you want a different falling speed.Input Manager:
Cursor Visibility:
lockCursor to false.Character Controller Height:
Camera Clipping:
Physics and Collisions:
With this basic controller set up, you can expand your game by:
Let me know if you have any questions or need further assistance setting up your first-person controller!
Not yet included in a published release.