- Home /
How to make a Fps sliding mechanic like in Far Cry?
I used a character controller to get the moving and gravity. I was trying days to add a sliding mechanic when the player crouches while running l(Like in Far Cry). Tried using rigidbody.AddForce(), characterController.Move() and transform.Translate() with Coroutines. But none of them worked as expected. Anyone have any suggestions on how to get this to work?Heres the code with the sliding part removed. Am I doing anything wrong? I'm new to Unity.
public class Player : MonoBehaviour
{
// Start is called before the first frame update
float mouseX,VInP,HInP;
[SerializeField]
float sens = 5f,MovementSpeed;
[SerializeField]
float gravity = -9.81f, cachedYval,jumpHeight;
[SerializeField]
LookY _verticalLook;
private bool isPaused;
[SerializeField]
CharacterController cc;
[SerializeField]
bool canRun = false, canJump = true,canMove,isCrouching,isRunning,isSliding,FinishedSliding;
private bool canSlide;
[SerializeField]
private float runSpeed,crouchSpeed;
[SerializeField]
Rigidbody rb;
[SerializeField]
Vector3 temp;
[SerializeField]
Animator anim;
void Start()
{
Cursor.lockState = CursorLockMode.Locked;
isPaused = false;
MovementSpeed = 10f;
canJump = true;
isCrouching = false;
isSliding = false;
}
// Update is called once per frame
void Update()
{
Pause();
if (!isPaused)
{
totalLook();
crouch();
Movement();
}
}
void totalLook()
{
mouseX = Input.GetAxis("Mouse X") * Time.deltaTime * sens;
transform.Rotate(Vector3.up * mouseX);
_verticalLook.VerticalLook();
}
private void Movement()
{
GetInput();
if (VInP >= 0&&!isCrouching)
{
canRun = true;
}
else
{
canRun = false;
}
if (canRun&&Input.GetKey(KeyCode.LeftShift)&&VInP>0)
{
VInP *= runSpeed;
isRunning = true;
canSlide = true;
}
else
{
isRunning = false;
}
Vector3 move = new Vector3(HInP, 0, VInP);
if (isCrouching)
{
move *= crouchSpeed;
}
Vector3 moveDirection=transform.transform.TransformDirection(move);
if (Input.GetKeyDown(KeyCode.Space)&&cc.isGrounded&&canJump&&!isCrouching)
{
cachedYval = Mathf.Sqrt(-2f*jumpHeight*gravity);
}
else if(cc.isGrounded)
{
cachedYval = 0f;
}
else
{
cachedYval += 2*gravity * Time.deltaTime;
}
moveDirection.y = cachedYval;
cc.Move(moveDirection * MovementSpeed * Time.deltaTime);
}
void Pause()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
isPaused = !isPaused;
if (isPaused)
{
Cursor.lockState = CursorLockMode.None;
}
else
{
Cursor.lockState = CursorLockMode.Locked;
}
}
}
void crouch()
{
if (Input.GetKeyDown(KeyCode.C))
{
Debug.Log("pressed c");
if (!isCrouching)
{
isCrouching = true;
anim.SetBool("crouching", true);
cc.center = new Vector3(cc.center.x, cc.center.y - .47f, cc.center.z);
cc.height /= 3;
}
else
{
Debug.Log(isCrouching);
isCrouching = false;
anim.SetBool("crouching", false);
cc.center = new Vector3(cc.center.x,cc.center.y+.47f,cc.center.z);
cc.height *= 3;
}
}`enter code here`
}
void GetInput()
{
VInP = Input.GetAxis("Vertical");
HInP = Input.GetAxis("Horizontal");
}
IEnumerator slide()
{
isSliding = true;
cc.Move(SlideDirection * 100 * Time.deltaTime);
yield return new WaitForSeconds(7f*Time.deltaTime);
canSlide=false;
isSliding = false;
Debug.Log("here");
}
}
Comment
Your answer
Follow this Question
Related Questions
How do I create WASD controls for a fps? 3 Answers
Vector3.Lerp is flinging my character all over the place! 2 Answers
Ultimate FPS not working correctly. 0 Answers
FPS mouse movement 0 Answers
FPS Flashlight Vertical Movement 1 Answer