Creating a Unity C# script for a character that moves forward at variable speeds and can switch between three lanes (left, center, right) with smooth transitions requires manipulating the Transform component alongside handling input for lane switching and jumping. Additionally, integrating animations, such as jumping, means controlling an Animator component based on player actions.
In this example, I'll guide you through creating a simple script that fulfills these requirements. This script assumes you're using a Rigidbody for movement and physics calculations, and it's important for the animator setup to have a boolean parameter named "isJumping" as mentioned.
Firstly, make sure your Unity project is prepared:
- Your character GameObject has a Rigidbody and Animator component attached.
- Your environment is set up to allow for clear lane distinction and movement.
Here's an example script you can use and modify according to your needs:
using UnityEngine;
public class CharacterController : MonoBehaviour
{
public float speed = 5f; // Forward movement speed
public float laneSwitchSpeed = 10f; // Speed of switching lanes
public float laneDistance = 4f; // Distance between lanes
private int currentLane = 1; // 0 = Left, 1 = Center, 2 = Right
private Vector3 targetPosition;
private Rigidbody rb;
private Animator animator;
public bool isGrounded = true;
void Start()
{
rb = GetComponent<Rigidbody>();
animator = GetComponentInChildren<Animator>();
targetPosition = transform.position;
}
void Update()
{
// Forward movement
Vector3 forwardMove = transform.forward * speed * Time.deltaTime;
rb.MovePosition(rb.position + forwardMove);
LaneInput();
// Jump input
if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
{
Jump();
}
}
void FixedUpdate()
{
// Smooth lane switching mechanic
Vector3 newPosition = Vector3.Lerp(rb.position, targetPosition, Time.fixedDeltaTime * laneSwitchSpeed);
rb.MovePosition(new Vector3(newPosition.x, rb.position.y, newPosition.z));
}
void LaneInput()
{
if (Input.GetKeyDown(KeyCode.LeftArrow) && currentLane > 0)
{
currentLane--;
UpdateLane();
}
else if (Input.GetKeyDown(KeyCode.RightArrow) && currentLane < 2)
{
currentLane++;
UpdateLane();
}
}
void UpdateLane()
{
float targetX = currentLane switch
{
0 => -laneDistance,
1 => 0,
2 => laneDistance,
_ => 0,
};
targetPosition = new Vector3(targetX, rb.position.y, rb.position.z);
}
void Jump()
{
// Assuming you're using a trigger or boolean in your animator to handle the jump animation
animator.SetBool("isJumping", true);
isGrounded = false;
rb.AddForce(Vector3.up * 5f, ForceMode.VelocityChange); // Modify the force value as needed
}
// Sample OnCollisionEnter method to detect when the character hits the ground
void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Ground")) // Make sure your ground has the tag "Ground"
{
isGrounded = true;
animator.SetBool("isJumping", false);
}
}
}
Assign this script to your character GameObject. Make sure:
- The Rigidbody component's
Use Gravity is checked for proper jumping physics.
- Your ground GameObjects have a "Ground" tag so the script can correctly detect when the player is grounded.
- Adjust
speed, laneSwitchSpeed, laneDistance, and the force applied in the Jump method according to your game's requirements and testing.
This script provides the basic functionality for moving forward, switching lanes smoothly, and jumping with animation control through the Animator's boolean parameter isJumping. Adjustments may be necessary depending on the specific setup of your project, character model, and physics settings.