Using "AddForce" with a Character Controller
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;
}
}
}
Answer by WerewoofPrime · Sep 13, 2021 at 11:11 PM
I think I found something! I have this same exact issue. do you think this will help? https://docs.unity3d.com/ScriptReference/CharacterController.Move.html
move is a very basic property of character controllers that allows them to move basically, in order to create a force with it, you'll need to create a force vector3 variable and then use it in Controller.Move(), making sure to multiply it by Time.deltaTime and keeping it in update, after that you can use Vector3.Lerp or Vector3.MoveTowards to bring it down to 0...and then create a function to set the force vector3 to the desired value....and it will automatically play out like a force....
you can also use a normalized vector3 for direction and a float for magnitude
Your answer
Follow this Question
Related Questions
Character Controller to create a lateral dash (1st person) - Keep Momentum by Adding Force? 0 Answers
AddForce in Character Controller problem 0 Answers
CharacterController.Move isn't setting collision flags 1 Answer
How can I adapt this character controller movement script to allow movement in mid air? 0 Answers