Jumping Development Tutorials, Guides & Insights
Unlock 1+ expert-curated jumping tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your jumping skills on DeveloperBreeze.
Adblocker Detected
It looks like you're using an adblocker. Our website relies on ads to keep running. Please consider disabling your adblocker to support us and access the content.
Unity Player Controller Blueprint
Code August 12, 2024
csharp
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);
}
}- 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.