- Home /
Slide in one set direction
I have a movement script with a slide script in it on a character controller, the script works off of a timer and works great except for the fact you can move the direction of the slide to whatever your mouse is pointed at. I would appreciate help in making it so that the direction of the slide is the direction it continues at until it's done. I also condensed this so you no one would have to look through 300 lines of code so if I'm missing something, let me know.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float slideSpeed = 7f;
public float slideTimer = 1.2f;
float x = 0f;
float z = 0f;
static bool isSliding = false;
static bool canSlide = false;
bool isGrounded;
Vector3 velocity;
void Update()
{
//Basic movement-------------------------------------------------------------------
isGrounded = Physics.CheckSphere(groundCheck.position, groundDistance, groundMask);
if (isGrounded && velocity.y < 0 && !isSliding)
{
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
if (velocity.y < 0)
{
velocity.y = -2f;
}
}
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * baseSpeed * Time.deltaTime);
//Slide---------------------------------------------------
if (Input.GetKeyDown(KeyCode.F) && canSlide && isGrounded)
{
Slide();
StartCoroutine(SlideTimer());
}
//Slide-----------------------------------------
void Slide()
{
playerCol.height = crouchHeight;
baseSpeed = slideSpeed;
isSliding = true;
canSprint = false;
isSprinting = false;
isProne = false;
canJump = false;
isCrouched = false;
canSlide = false;
slideTimer += Time.deltaTime;
}
IEnumerator SlideTimer()
{
yield return new WaitForSeconds(slideTimer);
if (isSliding)
{
Crouch();
}
}
Answer by Ermiq · Mar 01, 2021 at 06:33 AM
The script doesn't update x
and y
if the character isSliding
is true
.
So, the only reason why the direction is getting changed is the transform.right
and transform.forward
are changed somewhere. Most likely you turn the character's transform to the mouse position, and Vector3 move = transform.right * x + transform.forward * z;
is turning accordingly.
If I'm correct, you're saying that I need to update the x and y position while the isSliding is true so that my Vector3 move follows the updated x and y. If so, I've tried messing around with it but can't really find a way to do this as I'm pretty new. If you could, could I get an example ? I understand what you're saying but I'm just having a hard time. Also, here's what I currently have:
if (!isSliding)
{
Vector3 move = transform.right * x + transform.forward * z;
controller.Move(move * baseSpeed * Time.deltaTime);
}
if (isSliding)
{
Vector3 move = transform.forward;
controller.Move(move *baseSpeed * Time.deltaTime);
}
Uhm... I meant quite the opposite actually.
If I understand your question correctly, the direction of the slide is currently dependent on the mouse position, so the character is turning to the mouse point, and you don't want it to do that while he's in the sliding state. You need the character to slide in one direction, the direction that he has been moving at right before the slide start, and he should not turn to the mouse point during the slide.
Is that correct?
If so, then you need to disable transform
rotation when isSliding
is true
. Because the slide direction in your code is built from 2 parameters: the transform
's direction and x, z
inputs.
Obviously, the code you have provided doesn't affect the character direction nor slide direction while isSliding
is true
. And the x, z
input is ignored while isSliding
is true
. So, the direction is getting changed because the transform
direction is getting changed somewhere else in your project.
As an option, if you actually would like to have the transform
rotate even while sliding, but the actual move
direction should remain the same during the slide no matter where the mouse is pointing at, you can store the move
as global variable and not change it while isSliding
is true
. Like this:
Vector3 move;
void Update()
{
if (isGrounded && velocity.y < 0 && !isSliding)
{
x = Input.GetAxis("Horizontal");
z = Input.GetAxis("Vertical");
if (velocity.y < 0)
{
velocity.y = -2f;
}
move = transform.right * x + transform.forward * z;
// notice that here the move vector is updated only if the character is not sliding.
}
// if the isSliding = true, the move vector will remain the same as it was in the previous frame when isSliding was false, therefore the resulting slide direction will remain the same during the slide, independent of the x,z and transform's direction
controller.Move(move * baseSpeed * Time.deltaTime);
}
I get it now, this solved my problem perfectly. Interesting how I got that so backwards but I appreciate your time.