Unity Character Controller 3D | Unity Tutorial
Learn to create a 3D character controller in Unity! This quick tutorial covers setting up player movement, jumping, and basic interactions using Rigidbody physics and C# scripting. Perfect for beginners looking to get started with character control in Unity.
WATCH FULL TUTORIAL ON YOUTUBE
=========================================================
Unity Projects - ON SALE!!!
1. Water Sort Puzzle 9000 Levels
2. Ball Sort Puzzle 9000 Levels
3. Sling Shot
=========================================================
Script:
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public float moveSpeed = 5f; // Speed of movement
public float jumpForce = 10f; // Force of the jump
public Transform groundCheck; // Position of the ground check
public LayerMask groundLayer; // Layer mask to determine what is considered ground
public float groundCheckRadius = 0.2f; // Radius of the ground check sphere
public KeyCode jumpKey = KeyCode.Space; // Key to jump
private Rigidbody rb;
private bool isGrounded;
void Start()
{
rb = GetComponent<Rigidbody>();
rb.freezeRotation = true; // Prevent rotation due to physics
}
void Update()
{
// Check if the player is grounded
isGrounded = Physics.CheckSphere(groundCheck.position, groundCheckRadius, groundLayer);
// Get input from the joystick
float horizontal = Input.GetAxis("Horizontal");
float vertical = Input.GetAxis("Vertical");
// Movement direction based on input
Vector3 movement = new Vector3(horizontal, 0.0f, vertical).normalized;
// Move the player
Vector3 moveDirection = movement * moveSpeed * Time.deltaTime;
rb.MovePosition(transform.position + moveDirection);
// Jumping
if (isGrounded && Input.GetKeyDown(jumpKey))
{
rb.velocity = new Vector3(rb.velocity.x, jumpForce, rb.velocity.z); // Set jump force
}
}
}