0% found this document useful (0 votes)
2 views

module 1

The document provides a comprehensive guide on implementing character controllers in both Unity (C#) and Unreal Engine (Blueprints or C++). It covers steps for importing characters, setting up controllers, handling input, and various types of character controllers, including kinematic and physics-based. Additionally, it discusses concepts like unlimited movement, abilities, input sources, and resources, along with example code snippets for practical implementation.

Uploaded by

Kamalin Dany
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

module 1

The document provides a comprehensive guide on implementing character controllers in both Unity (C#) and Unreal Engine (Blueprints or C++). It covers steps for importing characters, setting up controllers, handling input, and various types of character controllers, including kinematic and physics-based. Additionally, it discusses concepts like unlimited movement, abilities, input sources, and resources, along with example code snippets for practical implementation.

Uploaded by

Kamalin Dany
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

1.

Unity (C#)

a) Importing the Character

1. Import the Model: Drag and drop your character's model (FBX, OBJ,
etc.) into the Assets folder in Unity.

2. Import Animations: If the model comes with animations, Unity will


automatically import them if the model is in FBX format. Make sure the
"Animation" tab in the import settings is configured correctly.

3. Set Up Materials/Textures: If needed, assign materials and textures


to the model.

b) Adding the Character Controller

1. Create a Player Controller Script: You'll need a script to control the


character. Create a new C# script, for example, PlayerController.cs.

Example PlayerController.cs for basic movement:

csharp

Copy code

using UnityEngine;

public class PlayerController : MonoBehaviour

public float moveSpeed = 5f;

public float turnSpeed = 700f;

void Update()

float horizontal = Input.GetAxis("Horizontal");

float vertical = Input.GetAxis("Vertical");

Vector3 direction = new Vector3(horizontal, 0, vertical).normalized;

if (direction.magnitude >= 0.1f)


{

transform.Translate(direction * moveSpeed * Time.deltaTime,


Space.World);

2. Add a Character Controller Component:

o Select the imported character in the scene.

o In the Inspector, click on Add Component and add a


CharacterController component. This component will handle
collisions and movement.

3. Assign the Script:

o Drag and drop the PlayerController script onto your character in


the scene.

o Set up the controller settings as needed.

4. Link the Camera:

o You might want to follow the player with the camera. You can
attach a camera and set it to follow the player either via script or
using a tool like Cinemachine for smooth camera control.

c) Assign the Controller (AI or Player):

 For AI, you may use NavMeshAgent to control pathfinding and


movement.

 For player input, you can just rely on the script as shown above.

2. Unreal Engine (Blueprints or C++)

a) Importing the Character

1. Import the Model: Go to the Content Browser, click Import, and


select your character model (usually in FBX format).

2. Import Animations: If your character has animations, you can import


them in a similar manner and associate them with the character.
b) Setting Up the Character

1. Create a Character Blueprint:

o Right-click in the Content Browser, then choose Blueprint Class.

o Select Character as the parent class. This automatically includes


a CharacterMovementComponent that provides functionality like
walking, jumping, etc.

o Open the Blueprint and go to the Viewport to add the character's


mesh.

o Assign your character's skeletal mesh to the Mesh component in


the Blueprint.

2. Create a Player Controller Blueprint:

o You can create a custom Player Controller to handle input and


interactions.

o Right-click and create a Blueprint Class, choosing


PlayerController as the parent class.

3. Set Up Input Mapping:

o Go to Edit > Project Settings > Input.

o Define actions and axes for movement (e.g., MoveForward,


MoveRight, Jump).

4. Add Movement Logic (inside PlayerController or Character


Blueprint):

o Set up movement using the input actions (e.g., use Add


Movement Input node for WASD movement).

5. Assign the Controller:

o Open the World Settings and set your PlayerController as the


default.

o For a player character, set the Auto Possess Player setting in the
Character Blueprint to Player 0 (or whichever player number is
relevant).

A character controller in video games refers to the system or component


that handles the movement and interactions of a player-controlled character
in a 3D or 2D environment. It often includes handling input (like keyboard,
mouse, or gamepad), moving the character within the game world, and
interacting with the environment (collisions, physics, etc.).

Types of Character Controllers:

1. Kinematic Character Controller: This controller handles the


movement and interaction of the character manually, without relying
on the physics engine for movement. It's typically used for characters
with precise movements or behaviors, often found in platformers or
first-person games.

2. Physics-based Character Controller: This controller works in


tandem with the physics engine, allowing the character to be affected
by gravity, forces, and collisions in a more natural way. It's used in
games that require realistic or semi-realistic interactions, like RPGs or
action-adventure games.

Key Components of a Character Controller:

1. Movement: Handles translating player input into character


movement, often involving:

o Walking, running, jumping, crouching, and other forms of motion.

o Animation blending (running, idle, walking).

o Gravity and fall mechanics.

2. Collision Detection: Ensures that the character doesn't pass through


walls or other objects in the game world.

o Often uses colliders (boxes, spheres, capsules) for hit detection


and preventing overlaps.

o Requires fine-tuned physics and collision response for smooth


gameplay.

3. Input Handling: Maps user input (e.g., keyboard keys, controller


buttons) to actions like movement, attacking, or jumping.

o Input systems often distinguish between movement (e.g., WASD


or thumbstick) and actions (e.g., mouse button, controller
triggers).

4. Camera System: Although not part of the controller itself, a camera


system is often tied closely to the character controller for player
viewpoint. For example, third-person games typically allow the camera
to follow the character, while first-person games require the camera to
align with the character's perspective.

5. Animation System: Ensures that the character’s movement is synced


with animations, such as walking, running, and jumping. This could
involve:

o Animation blending (smoothly transitioning between animations


like idle and walking).

o Procedural animations (e.g., using inverse kinematics for foot


placement).

Common Character Controller Implementations:

1. Unity's CharacterController Component:

 Unity provides a built-in CharacterController component that handles


basic movement and collision detection. It’s typically used in third-
person or first-person games.

 It allows for basic functionality like walking, jumping, and slopes, but
leaves room for customization, such as adding advanced features like
climbing, swimming, or interacting with objects.

Example:

csharp

Copy code

using UnityEngine;

public class PlayerController : MonoBehaviour

public float moveSpeed = 5f;

public float jumpHeight = 2f;

private CharacterController controller;

void Start()
{

controller = GetComponent<CharacterController>();

void Update()

MovePlayer();

void MovePlayer()

float moveX = Input.GetAxis("Horizontal");

float moveZ = Input.GetAxis("Vertical");

Vector3 move = transform.right * moveX + transform.forward * moveZ;

if (controller.isGrounded)

float ySpeed = 0f;

if (Input.GetButtonDown("Jump"))

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

move.y = ySpeed;

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


}

An unlimited character controller is not a standard term in game


development, but it could be interpreted in different ways depending on
context. Below are some possible interpretations and solutions for what an
"unlimited character controller" might mean:

1. Unlimited Movement (No Constraints)

If by "unlimited" you mean that the character is not bound by traditional


constraints such as gravity, boundaries, or maximum speed, then you would
essentially be designing a system where the character has free movement
across the game world, possibly without physical limitations.

Examples:

 No gravity: The character might be able to move in any direction


without being pulled down.

 No boundaries: The character could move beyond typical world


boundaries, such as walking off the map.

 Infinite speed: The character might not have a maximum movement


speed, allowing them to move as fast as possible.

Example: Unity Controller with Unlimited Movement

Here’s a basic Unity example that removes gravity and allows for free
movement in 3D space:

csharp

Copy code

using UnityEngine;

public class UnlimitedController : MonoBehaviour

public float moveSpeed = 10f;

public float rotateSpeed = 100f;


void Update()

// Get input from the player

float horizontal = Input.GetAxis("Horizontal");

float vertical = Input.GetAxis("Vertical");

float lift = 0f;

if (Input.GetKey(KeyCode.Space)) lift = 1f; // Move up

if (Input.GetKey(KeyCode.LeftControl)) lift = -1f; // Move down

// Calculate movement direction

Vector3 moveDirection = new Vector3(horizontal, lift,


vertical).normalized;

// Apply movement

transform.Translate(moveDirection * moveSpeed * Time.deltaTime);

// Rotate character with mouse input (looking around)

float horizontalRotation = Input.GetAxis("Mouse X") * rotateSpeed *


Time.deltaTime;

float verticalRotation = -Input.GetAxis("Mouse Y") * rotateSpeed *


Time.deltaTime;

transform.Rotate(0f, horizontalRotation, 0f); // Rotate character around


Y-axis

Camera.main.transform.Rotate(verticalRotation, 0f, 0f); // Rotate


camera up/down

}
}

In this case:

 No gravity is applied to the character.

 The character can move freely in 3D space, including up and down


(using Space and Left Control for upward and downward movement,
respectively).

 The movement is "unlimited" in the sense that there are no constraints


on direction or speed.

2. Unlimited Abilities (Multiple Skills or Actions)

Another interpretation of an "unlimited controller" could be a system that


allows the character to perform an unlimited number of actions or abilities
(like attacks, spells, or other interactions). For example, a controller that lets
the player use unlimited skills or spells without cooldowns or limits.

Example: Unity Controller with Unlimited Abilities

Here’s a basic example where the character can cast a spell (or ability) as
many times as they want:

csharp

Copy code

using UnityEngine;

public class UnlimitedAbilities : MonoBehaviour

public GameObject spellPrefab;

public Transform spellSpawnPoint;

void Update()

if (Input.GetKeyDown(KeyCode.F)) // Cast spell on F key press


{

CastSpell();

void CastSpell()

// Instantiate spell at spawn point

Instantiate(spellPrefab, spellSpawnPoint.position,
spellSpawnPoint.rotation);

This script allows the player to cast as many spells as they want by pressing
the F key without any cooldown or resource cost. The spell is instantiated
every time the player presses the key, creating an "unlimited" ability use
scenario.

3. Unlimited Input Sources (Multiple Controllers)

If by "unlimited controller" you mean the ability to accept input from multiple
sources (e.g., multiple gamepads, keyboards, or other input devices), then
you would design a system that can handle input from a potentially unlimited
number of controllers at once.

In this case, you might need to handle input from various devices, such as:

 Multiple players on a local multiplayer system

 Inputs from both keyboard and gamepads simultaneously

 Mapping multiple devices to a single player (e.g., touch input +


controller)

Example: Unity Input System for Multiple Players


Unity’s Input System package allows you to easily manage input from
multiple devices. Here's a very basic example of how you could set up
multiple player controls with the new Input System:

csharp

Copy code

using UnityEngine;

using UnityEngine.InputSystem;

public class MultiPlayerController : MonoBehaviour

private PlayerInput playerInput;

private Vector2 movementInput;

void Awake()

playerInput = GetComponent<PlayerInput>();

void Update()

// Get movement input from the current player

movementInput = playerInput.actions["Move"].ReadValue<Vector2>();

// Handle movement here based on the input

Vector3 move = new Vector3(movementInput.x, 0, movementInput.y);

transform.Translate(move * Time.deltaTime * 5f);

}
}

With the Unity Input System, you can handle multiple players and devices
by configuring action maps and bindings that allow each player to use
different input devices. For instance:

 Player 1 could use the keyboard.

 Player 2 could use a gamepad.

You can even add touch input or motion controllers for additional
interaction, allowing the game to scale for different devices.

4. Unlimited Resources (No Limits on Health, Ammo, etc.)

Another variant of "unlimited" could refer to unlimited resources such as


health, ammo, or energy in a character controller. In this case, you might
design a system where the character can perform actions (like shooting,
using abilities, or taking damage) without running out of resources or being
affected by limits (e.g., health doesn’t decrease, ammo doesn’t run out).

Example: Unity Character with Unlimited Health and Ammo

csharp

Copy code

using UnityEngine;

public class UnlimitedResources : MonoBehaviour

public float health = 100f;

public int ammo = 100;

void Update()

// Example: Player takes damage, but health never decreases

if (Input.GetKeyDown(KeyCode.H))
{

TakeDamage(10);

// Example: Player shoots, but ammo never decreases

if (Input.GetKeyDown(KeyCode.Mouse0)) // Left mouse click

Shoot();

void TakeDamage(float amount)

// Health doesn't actually decrease

health = Mathf.Max(health, health - amount); // No negative health

void Shoot()

// Ammo doesn't decrease

Debug.Log("Shooting... (Unlimited Ammo)");

Keyboard controller

In Unity, a keyboard controller typically refers to a script or system that


handles user input from a physical keyboard to control various elements of a
game or application, such as player movement, camera control, or triggering
actions. This can be achieved using Unity's Input system (old or new), and
often involves writing scripts in C# to interpret key presses or key releases.

Here’s a general guide on how to set up a keyboard controller in Unity:

1. Basic Keyboard Input with Unity's Input System (Legacy)

In the legacy Input system, you can use the Input.GetKeyDown(),


Input.GetKey(), and Input.GetKeyUp() methods to detect key presses and
control game elements.

Example: Simple Player Movement

This script allows a player object to move based on keyboard input.

using UnityEngine;

public class PlayerController : MonoBehaviour

public float moveSpeed = 5f;

void Update()

float horizontal = Input.GetAxis("Horizontal"); // A/D or Arrow keys

float vertical = Input.GetAxis("Vertical"); // W/S or Arrow keys

Vector3 movement = new Vector3(horizontal, 0f, vertical);

transform.Translate(movement * moveSpeed * Time.deltaTime);

Explanation:

 Input.GetAxis("Horizontal"): Returns a value between -1 and 1 based


on the arrow keys or WASD keys (left-right).
 Input.GetAxis("Vertical"): Similar for up-down movement with W/S or
arrow keys.

 transform.Translate(): Moves the object based on the input.

This is a simple script where the player moves in 3D space, but it can be
adjusted for 2D or other purposes as well.

2. Using Unity's New Input System (Recommended)

Unity introduced a new Input System package, which provides more flexible
and powerful ways to handle input from various devices, including
keyboards, mice, gamepads, etc. You can enable this system through
Window > Package Manager and installing the "Input System" package.

Example: Player Movement Using New Input System

First, make sure the Input System package is installed and set it as the active
input handling method:

 Edit > Project Settings > Player > Other Settings > "Active Input
Handling" set to Both or Input System Package (New).

Now, use the new Input System for reading keyboard input.

using UnityEngine;

using UnityEngine.InputSystem;

public class PlayerController : MonoBehaviour

public float moveSpeed = 5f;

private Vector2 movementInput;

// This method is called when the player presses the movement keys

public void OnMove(InputAction.CallbackContext context)

{
movementInput = context.ReadValue<Vector2>();

void Update()

Vector3 movement = new Vector3(movementInput.x, 0f,


movementInput.y);

transform.Translate(movement * moveSpeed * Time.deltaTime);

Explanation:

 InputAction: The OnMove method is connected to an InputAction


(configured in the Input Actions Asset), which handles the movement
input from the keyboard.

 Vector2: The movement input is stored as a 2D vector, where x


corresponds to horizontal movement and y to vertical movement.

To complete the setup:

1. Go to Edit > Project Settings > Input System Package and set up
an Input Actions asset (you can create one in the Assets folder).

2. Add a "Move" action to this asset that binds to the WASD or arrow
keys, and link it to the OnMove function in your script.

3. Handling Keyboard Input for Specific Actions

You can also map keys to specific actions, such as jumping, shooting, or
interacting with objects.

Example: Jumping with Spacebar (Legacy Input System)

csharp

Copy code

using UnityEngine;
public class PlayerJump : MonoBehaviour

public float jumpForce = 10f;

private Rigidbody rb;

void Start()

rb = GetComponent<Rigidbody>();

void Update()

if (Input.GetKeyDown(KeyCode.Space)) // Detects when the spacebar is


pressed

rb.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);

This script applies an upward force (simulating a jump) when the spacebar is
pressed.

4. Handling Multiple Keys (Complex Input)

If you want to handle complex input, such as multiple keys pressed at once
(e.g., moving diagonally with WASD), you can check for specific key
combinations.

Example: Multiple Key Input (Legacy)


csharp

Copy code

using UnityEngine;

public class PlayerDiagonalMovement : MonoBehaviour

public float moveSpeed = 5f;

void Update()

float horizontal = 0f;

float vertical = 0f;

if (Input.GetKey(KeyCode.W)) vertical += 1;

if (Input.GetKey(KeyCode.S)) vertical -= 1;

if (Input.GetKey(KeyCode.A)) horizontal -= 1;

if (Input.GetKey(KeyCode.D)) horizontal += 1;

Vector3 movement = new Vector3(horizontal, 0f, vertical);

transform.Translate(movement * moveSpeed * Time.deltaTime);

This script checks for each individual key and allows diagonal movement if
two keys are pressed at once (e.g., W and D for moving up-right).

You might also like