I’m making a 3D game in which I want the player to dash in the direction they are facing when I press a button. However, want to use a character controller rather than a rigidbody so I know I can’t just use AddForce. Does anyone know how I can create short dash for my player in the direction they are facing?
Here is my movement script if that is helpful:
public class PlayerController : MonoBehaviour
{
Vector3 velocity;
public float gravity = -25f;
public Transform groundCheck;
public float groundDistance = 0.25f;
public LayerMask groundMask;
bool isGrounded;
public float turnSmoothTime = 0.07f;
float turnSmoothVelocity;
void Update()
{
//Character Movement
float horizontal = Input.GetAxisRaw("Horizontal");
float vertical = Input.GetAxisRaw("Vertical");
Vector3 direction = new Vector3(horizontal, 0f, vertical).normalized;
//Character Rotation
if (direction.magnitude >= 0.1f)
{
float targetAngle = Mathf.Atan2(direction.x, direction.z) * Mathf.Rad2Deg + cam.eulerAngles.y;
float angle = Mathf.SmoothDampAngle(transform.eulerAngles.y, targetAngle, ref turnSmoothVelocity, turnSmoothTime);
transform.rotation = Quaternion.Euler(0f, angle, 0f);
Vector3 moveDir = Quaternion.Euler(0f, targetAngle, 0f) * Vector3.forward;
controller.Move(moveDir.normalized * movementSpeed * Time.deltaTime);
}
//Gravity
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
//Ground Check
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0)
{
velocity.y = -1f;
}
}
}