キャラクターにダッシュ機能を追加するスクリプトです。通常の移動速度から一時的に速度を上げることができます。
using UnityEngine;
public class CharacterDash : MonoBehaviour
{
public float normalSpeed = 5f;
public float dashSpeed = 10f;
public float dashDuration = 0.5f;
private float dashTime;
private bool isDashing;
void Update()
{
float speed = isDashing ? dashSpeed : normalSpeed;
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
transform.Translate(movement * speed * Time.deltaTime, Space.World);
if (Input.GetKeyDown(KeyCode.LeftShift))
{
isDashing = true;
dashTime = dashDuration;
}
if (isDashing)
{
dashTime -= Time.deltaTime;
if (dashTime <= 0)
{
isDashing = false;
}
}
}
}
コメント