<!DOCTYPE html>
Sprinting Mechanics
To add a sprinting mechanic, we need to modify our script to consider an additional input for sprinting:
csharp
void Update() {
float moveHorizontal = Input.GetAxis(“Horizontal”);
float moveVertical = Input.GetAxis(“Vertical”);
bool isSprinting = Input.GetKey(KeyCode.LeftShift);
Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
float speed = (isSprinting) ? sprintSpeed : walkSpeed;
transform.position += movement * speed * Time.deltaTime;
// Rotate towards the direction of movement
Vector3 targetDirection = movement.normalized;
transform.rotation = Quaternion.LookRotation(targetDirection);
}
Here, we’ve added a boolean variable `isSprinting` that checks if the left shift key is being pressed. If it is, the character moves at a faster speed (sprintSpeed). This script assumes you have already defined sprintSpeed and walkSpeed variables in your script.
Jumping and Gravity
To incorporate jumping, we need to consider gravity and collision detection:
csharp
void Update() {
float moveHorizontal = Input.GetAxis(“Horizontal”);
float moveVertical = Input.GetAxis(“Vertical”);
bool isSprinting = Input.GetKey(KeyCode.LeftShift);
bool isJumping = Input.GetKeyDown(KeyCode.Space);
bool onGround = false; // Assuming we have a ground tag for collision detection
Vector3 movement = new Vector3(moveHorizontal, 0f, moveVertical);
float speed = (isSprinting) ? sprintSpeed : walkSpeed;
transform.position += movement * speed * Time.deltaTime;
if (isJumping && onGround) {
GetComponent().AddForce(new Vector3(0f, jumpForce, 0f), ForceMode.Impulse);
onGround = false;
}
// Apply gravity
if (!onGround) {
GetComponent().AddForce(new Vector3(0f, -gravity, 0f), ForceMode.Gravitation);
}
// Rotate towards the direction of movement
Vector3 targetDirection = movement.normalized;
transform.rotation = Quaternion.LookRotation(targetDirection);
}
In this script, we’ve added a boolean variable `onGround` to check if the character is on the ground and an Input.GetKeyDown event for jumping. When the space key is pressed while on the ground, the character jumps by applying an upward force (jumpForce). We also apply gravity when the character is in the air.
Obstacle Navigation and Pathfinding
For more complex scenarios, such as navigating through obstacles or following a path, we can use Unity’s NavMesh system:
csharp
void Update() {
Vector3 destination = waypoints[currentWaypointIndex];
transform.position = Vector3.MoveTowards(transform.position, destination, speed * Time.deltaTime);
if (Vector3.Distance(transform.position, destination) waypoints.Length) {
// Reached the end of the path
Debug.Log(“Reached the end!”);
}
}
}
In this script, we’ve replaced the movement calculation with a Vector3.MoveTowards function that moves the character towards a destination (waypoint). We also check if the character has reached the current waypoint and, if so, move to the next one in the array. This assumes you have defined an array of waypoints (Vector3[]) and a currentWaypointIndex variable in your script.
Summary
With these building blocks, you can create captivating character movement experiences in Unity 3D. From simple forward movement to complex navigation systems, the possibilities are endless.