Character Controller to create a lateral dash (1st person) - Keep Momentum by Adding Force?
Hi, hopefully somebody can help me with my problem.
My main goal is to make my 1st Person Character Controller to dash sideway (quick lateral movement) when double taping A or D (within a timeframe). That said, i'm using the character controller component in order to move my player.
So far, I've been able to make that dash move that I want by increasing the speed(by multiplying with a sort of dashSpeedMultiplier) of my moveVector.x that is applied into my CharacterController.Move method. Unfortunately, this vector.x takes into account my Input.GetAxis("Horizontal"). This means that my dash cancels out as soon as I relase my A or D key. If I want to make a complete dash move like intended, I need to hold my A or D key until I reach the maximum distance possible with this increasing speed. When I release A or D in the middle of the dash. It cancels out mid air.
Here is a video of the behaviour I'm talking about. I would like the dash to feel like in the beginning of the video, not a the end where it sort of cancels out as I release the key in "midair" of my dash.
What I would like is whatever the A or D control is held or release, the dash should always feel the same (like if holding, even though we don't). It's like it should not take into account the Input.GetAxis,as when releasing the key, this would turn the speed to 0.
You can see below my script for reference. I have tried different things, like adding a smoothdamp to my moveVector.x (my goal was to gradually decrease the value of my Input from 1 to 0. This didn't help, maybe because I didn't use it well but overall it is affecting all my movement in a unintended way.
I tried to add a bool when strafin, and have two type of vector to put in the _controller.Move(), but it was not working too. It feels like manipulating the speed of my player may not be the best way.
Is there any way to add a force that would "push" my player on his side for creating the dashing behaviour. While this force is applied, I could lock the movement of my player until a specified time or until the controller is grounded (if for example, the player is dashing off a cliff, I would still like to keep the momentum until he's touching the ground).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMove : MonoBehaviour
{
private CharacterController _controller;
private float _verticalVelocity;
[SerializeField] private float _gravity = 14.0f;
[SerializeField] private float _jumpForce = 10.0f;
[SerializeField] private float _speed = 4.0f;
[SerializeField] private float _strafeJump = 2.0f;
[SerializeField] private float _strafeSpeedMultiplier = 6.0f;
[SerializeField] private float _damping = 1.0f;
private bool _canDoubleJump = false;
private bool _canStrafeLeft = false;
private bool _canStrafeRight = false;
Vector3 moveVector = Vector3.zero;
Vector3 strafeVector = Vector3.zero;
// Start is called before the first frame update
void Start()
{
_controller = GetComponent<CharacterController>();
_strafeSpeedMultiplier = 1.0f;
}
// Update is called once per frame
void Update()
{
//if player is on the ground
if (_controller.isGrounded)
{
//first, apply gravity
_verticalVelocity = -_gravity * Time.deltaTime;
//when player land on ground, his double jump ability is false
_canDoubleJump = false;
//reset _strafeSpeedMultiplier if _canStrafe
if (!_canStrafeLeft || !_canStrafeRight)
{
_strafeSpeedMultiplier = 1.0f;
}
//activate strafe left by first releasing A
if (Input.GetKeyUp(KeyCode.A))
{
//on pressing A again within timeframe of IEnumerator (StrafeReset), you can strafe on left
_canStrafeLeft = true;
StartCoroutine(StrafeReset());
}
//activate strafe right by first releasing D
if (Input.GetKeyUp(KeyCode.D))
{
//on pressing D again within timeframe of IEnumerator (StrafeReset), you can strafe on right
_canStrafeRight = true;
StartCoroutine(StrafeReset());
}
//strafe left double pressing A
if (Input.GetKeyDown(KeyCode.A) && _canStrafeLeft)
{
Strafe();
//strafe on left side
_canStrafeLeft = false;
}
//strafe right double pressing D
if (Input.GetKeyDown(KeyCode.D) && _canStrafeRight)
{
Strafe();
//strafe on right side
_canStrafeRight = false;
}
//Jump
if (Input.GetKeyDown(KeyCode.Space))
{
_verticalVelocity = _jumpForce;
//set double jump to true when the player has jump;
_canDoubleJump = true;
}
}
else
//if not grounded
{
//apply gravity
_verticalVelocity -= _gravity * Time.deltaTime;
//apply double jump
if (_canDoubleJump && Input.GetKeyDown(KeyCode.Space))
{
_verticalVelocity = _jumpForce;
//you cannot triple jump...
_canDoubleJump = false;
}
}
//MOVE THE PLAYER
//update each axis for movement
moveVector.x = Input.GetAxis("Horizontal") * _speed * _strafeSpeedMultiplier;
moveVector.z = Input.GetAxis("Vertical") * _speed;
//apply gravity on y axis
moveVector.y = _verticalVelocity;
//rotate for local space of the camera
moveVector = transform.transform.TransformDirection(moveVector);
//finally, move the player
_controller.Move(moveVector * Time.deltaTime);
}
//reset the possibility to strafe if it's too long between the double press (A & D). timeframe of 0.08sec
IEnumerator StrafeReset()
{
yield return new WaitForSeconds(0.08f);
_canStrafeLeft = false;
_canStrafeRight = false;
}
//strafing method
private void Strafe()
{
//make a little jump
_verticalVelocity = _strafeJump;
//increase speed for dashing effect
_strafeSpeedMultiplier = 6.0f;
}
}
Your answer
Follow this Question
Related Questions
Using "AddForce" with a Character Controller 1 Answer
AddForce and Momentum 1 Answer
need help badly 2 Answers
AddForce in Character Controller problem 0 Answers
How to create smooth jump with rigidbody.addforce? 2 Answers