Published on August 12, 2024By DeveloperBreeze

Unity Player Controller Blueprint

using UnityEngine;

// Basic player controller for Unity
[RequireComponent(typeof(CharacterController))]
public class PlayerController : MonoBehaviour
{
    [Header("Player Settings")]
    public float moveSpeed = 5f;
    public float jumpHeight = 2f;
    public float gravity = -9.81f;
    public Transform cameraTransform;

    private CharacterController characterController;
    private Vector3 velocity;
    private bool isGrounded;

    void Start()
    {
        characterController = GetComponent<CharacterController>();
    }

    void Update()
    {
        MovePlayer();
        RotateCamera();
    }

    void MovePlayer()
    {
        // Check if the player is on the ground
        isGrounded = characterController.isGrounded;
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f; // A small negative value to keep the player grounded
        }

        // Get input for movement
        float moveX = Input.GetAxis("Horizontal");
        float moveZ = Input.GetAxis("Vertical");

        // Calculate movement direction
        Vector3 move = transform.right * moveX + transform.forward * moveZ;
        characterController.Move(move * moveSpeed * Time.deltaTime);

        // Jumping logic
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }

        // Apply gravity
        velocity.y += gravity * Time.deltaTime;
        characterController.Move(velocity * Time.deltaTime);
    }

    void RotateCamera()
    {
        // Get input for mouse movement
        float mouseX = Input.GetAxis("Mouse X");
        float mouseY = Input.GetAxis("Mouse Y");

        // Rotate player along the y-axis
        transform.Rotate(Vector3.up * mouseX);

        // Rotate camera along the x-axis
        cameraTransform.Rotate(Vector3.left * mouseY);
    }
}

Features of the Blueprint

    • Character Controller: This script uses Unity's CharacterController component to handle player movement and collision detection. Make sure to attach this component to your player object.

    • Movement: The player can move forward, backward, and strafe using the keyboard inputs. The MovePlayer method handles this.

    • Jumping: Basic jumping mechanics are implemented, allowing the player to jump if they are grounded.

    • Gravity: Simulated gravity ensures the player falls back to the ground after jumping.

    • Camera Rotation: Mouse input is used to rotate the player character and camera, providing a first-person or third-person view depending on the setup.

Usage Instructions

    • Attach Script: Add this script to your player GameObject in Unity.

    • Configure Components: Ensure that a CharacterController component is also attached to the same GameObject.

    • Camera Transform: Assign the main camera or a specific camera transform to the cameraTransform field in the Unity Inspector.

    • Tweak Settings: Adjust moveSpeed, jumpHeight, and gravity values in the Inspector to fit your game's requirements.

Extension Ideas

  • Animation Integration: Add animator components and trigger animations based on movement and jump states.

  • Advanced Physics: Integrate more complex physics interactions, such as slopes or surface friction.

  • Networking: Adapt the controller for multiplayer environments using Unity’s networking solutions.

This blueprint provides a starting point for building and customizing player controllers in Unity projects. It can be expanded and modified to fit various game genres and mechanics.

Comments

Please log in to leave a comment.

Continue Reading:

Calculate Sum of Numbers in Array

Published on January 26, 2024

csharp

Unity Inventory System using Scriptable Objects

Published on August 12, 2024

csharp

Build a Multiplayer Game with Python and WebSockets

Published on December 10, 2024

python

JavaScript in Modern Web Development

Published on December 10, 2024

javascript