- Home /
Dashing with charactercontorller
I'm trying to get my character to dash to where I am looking. I am using a character controller(CC). Here is my code.
using UnityEngine;
public class PlayerMovement : MonoBehaviour { public float moveSpeed; CharacterController controller; public float gravity = -9.81f; Vector3 velocity; public float jumpHeight = 4f;
public GameObject groundCheckObject;
public LayerMask groundMask;
public float sphereRad = .2f;
float dashCounter;
float initialDashTime = .1f;
bool dash;
int dashes = 0;
float dashCoolDown;
float initialDashCool = 3f;
public float hangtime = .1f;
float hangTimeCounter;
void Start()
{
controller = GetComponent<CharacterController>();
dashCounter = initialDashTime;
dashCoolDown = initialDashCool;
hangTimeCounter = hangtime ;
}
void Update()
{
bool grounded = Physics.CheckSphere(groundCheckObject.transform.position, sphereRad, groundMask);
velocity.y += gravity * Time.deltaTime;
StartDash();
EndDash();
DoDash();
if (grounded)
{
velocity.y = -2f;
}
if (grounded)
{
hangTimeCounter = hangtime;
}
else
{
hangTimeCounter -= .90f * Time.deltaTime;
}
if (Input.GetKeyDown(KeyCode.Space) && hangTimeCounter >= 0f)
{
velocity.y = Mathf.Sqrt(jumpHeight * -2 * gravity);
}
float x = Input.GetAxisRaw("Horizontal");
float z = Input.GetAxisRaw("Vertical");
Vector3 move = transform.forward * z + transform.right * x;
move = move.normalized;
controller.Move(move * moveSpeed * Time.deltaTime);
controller.Move(velocity * Time.deltaTime);
}
void EndDash()
{
if (dashes >= 3)
{
dashCoolDown -= .4f * Time.deltaTime;
}
if (dashCoolDown <= 0f)
{
dashes = 0;
dashCoolDown = initialDashCool;
}
}
void DoDash()
{
if (dash == true && dashCounter >= 0f && dashes < 4)
{
moveSpeed = 100f;
}
else if (dashCounter <= 0f)
{
moveSpeed = 11f;
}
}
void StartDash()
{
if (Input.GetKey(KeyCode.LeftShift))
{
dash = true;
dashCounter -= .69f * Time.deltaTime;
}
else
{
dash = false;
moveSpeed = 11f;
dashCounter = initialDashTime;
}
if (Input.GetKeyDown(KeyCode.LeftShift))
{
dashes += 1;
}
}
}
This code is for my movement and dashing. It's pretty simple and very messy. Just increasing the speed for a short amount of time. I was planning to rotate my character depending on where I was looking in the Y axis for a split second the change speed which would result in the player dashing wherever they looked. The problem is I can't rotate the CC collider so this approach won't work. Any Ideas on how make the player dash wherever they look? Sorry for the bad and messy code, I'm new to coding.
Here is my CameraLook script
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CameraController : MonoBehaviour { public float sens; public Transform player; float xRotation = 0f;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
}
// Update is called once per frame
void Update()
{
float mouseX = Input.GetAxis("Mouse X") * sens * Time.deltaTime;
float mouseY = Input.GetAxis("Mouse Y") * sens * Time.deltaTime;
xRotation -= mouseY;
transform.localRotation = Quaternion.Euler(xRotation,0,0);
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
player.Rotate(Vector3.up * mouseX);
}
}
Thanks in advance!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Dash where your looking 1 Answer
super messed up speed 1 Answer
Using isGrounded with a RigidBody 2 Answers