module 1
module 1
Unity (C#)
1. Import the Model: Drag and drop your character's model (FBX, OBJ,
etc.) into the Assets folder in Unity.
csharp
Copy code
using UnityEngine;
void Update()
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.
For player input, you can just rely on the script as shown above.
o For a player character, set the Auto Possess Player setting in the
Character Blueprint to Player 0 (or whichever player number is
relevant).
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;
void Start()
{
controller = GetComponent<CharacterController>();
void Update()
MovePlayer();
void MovePlayer()
if (controller.isGrounded)
if (Input.GetButtonDown("Jump"))
move.y = ySpeed;
Examples:
Here’s a basic Unity example that removes gravity and allows for free
movement in 3D space:
csharp
Copy code
using UnityEngine;
// Apply movement
}
}
In this case:
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;
void Update()
CastSpell();
void CastSpell()
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.
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:
csharp
Copy code
using UnityEngine;
using UnityEngine.InputSystem;
void Awake()
playerInput = GetComponent<PlayerInput>();
void Update()
movementInput = playerInput.actions["Move"].ReadValue<Vector2>();
}
}
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:
You can even add touch input or motion controllers for additional
interaction, allowing the game to scale for different devices.
csharp
Copy code
using UnityEngine;
void Update()
if (Input.GetKeyDown(KeyCode.H))
{
TakeDamage(10);
Shoot();
void Shoot()
Keyboard controller
using UnityEngine;
void Update()
Explanation:
This is a simple script where the player moves in 3D space, but it can be
adjusted for 2D or other purposes as well.
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.
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;
// This method is called when the player presses the movement keys
{
movementInput = context.ReadValue<Vector2>();
void Update()
Explanation:
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.
You can also map keys to specific actions, such as jumping, shooting, or
interacting with objects.
csharp
Copy code
using UnityEngine;
public class PlayerJump : MonoBehaviour
void Start()
rb = GetComponent<Rigidbody>();
void Update()
This script applies an upward force (simulating a jump) when the spacebar is
pressed.
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.
Copy code
using UnityEngine;
void Update()
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;
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).