-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxBoxController.cs
More file actions
32 lines (21 loc) · 1.06 KB
/
Copy pathxBoxController.cs
File metadata and controls
32 lines (21 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Using UnityEngine;
public class CharacterController : MonoBehaviour
{
//To use this script with an Xbox controller, make sure the controller is connected to your computer.
//In Unity, go to Edit ->Project Settings->Input to configure the input axes.
// Set the ‘horizontalAxis’ and ‘verticalAxis’ variables in the script to the corresponding input axis names defined in the input settings. Attach script to character object in the Unity Editor and the character will move based on the input from the Xbox controller.
//XBox Controller Movements: Left ThumbStick
public float moveSpeed = 5f;
public string horizontalAxis = “Horizontal”;
public string verticalAxis = “Vertical”;
private void Update()
{
//Get controller input
float horizontalInput = Input.GetAxis(horizontalAxis);
float verticalInput = Input.GetAxis(verticalAxis);
//Calculate movement direction
Vector3 movement = new Vector3(horizontalInput, 0f, verticalInput).normalized;
//Move the character
transform.Translate(movement * moveSpeed * Time.deltaTime);
}
}