- Home /
How to do sliding like Crysis and/or FarCry?
Hi there, I am making an FPS and I want to feature a sliding mechanic like Crysis and some other games have where the player presses a button while sprinting and the character slides on the floor. I've already set up sprinting and crouching but I have no idea to do sliding. Any help would be appreciated, also, I am using the default CharacterMotor to move around. Thanks!
If you're using mecanim humanoid there is starter pack on asset store with sliding animations and you could use those.
im not using mecanim in using a fps character ive built. I dont really $$anonymous$$d about animations i can do that with the camera i just want the sliding
You somehow have to tap into the character controller and take over, implement something like a crouch with a run speed for a certain period of time, and disable other inputs for that time, then when the time is reached leave the character controller crouched. Here is a great example on how you can tap into the character controller and modify the variables : http://answers.unity3d.com/questions/164638/how-to-make-the-fps-character-controller-run-and-c.html
Answer by AlucardJay · Jan 05, 2013 at 04:03 AM
Using the crouch and run script by Aldo (awesome script), I have added sliding. Set slideSpeed and slideTimerMax. When sliding, the player can look around, but not change direction.
#pragma strict
var walkSpeed: float = 7; // regular speed
var crchSpeed: float = 3; // crouching speed
var runSpeed: float = 20; // run speed
private var chMotor: CharacterMotor;
private var ch: CharacterController;
private var tr: Transform;
private var height: float; // initial height
public var slideSpeed: float = 20; // slide speed
private var isSliding : boolean = false;
private var slideForward : Vector3; // direction of slide
private var slideTimer : float = 0.0;
public var slideTimerMax : float = 2.5; // time while sliding
function Start()
{
chMotor = GetComponent(CharacterMotor);
tr = transform;
ch = GetComponent(CharacterController);
height = ch.height;
}
function Update()
{
var h = height;
var speed = walkSpeed;
// - run and crouch -
if (ch.isGrounded && Input.GetKey("left shift") || Input.GetKey("right shift")) // press Shift to run
{
speed = runSpeed;
}
if (Input.GetKey("c")) // press C to crouch
{
h = 0.5 * height;
speed = crchSpeed; // slow down when crouching
}
// - slide -
if (Input.GetKeyDown("f") && !isSliding) // press F to slide
{
slideTimer = 0.0; // start timer
isSliding = true;
slideForward = tr.forward;
}
if (isSliding)
{
h = 0.5 * height; // height is crouch height
speed = slideSpeed; // speed is slide speed
chMotor.movement.velocity = slideForward * speed;
slideTimer += Time.deltaTime;
if (slideTimer > slideTimerMax)
{
isSliding = false;
}
}
// - apply movement modifiers -
chMotor.movement.maxForwardSpeed = speed; // set max speed
var lastHeight = ch.height; // crouch/stand up smoothly
ch.height = Mathf.Lerp(ch.height, h, 5 * Time.deltaTime);
tr.position.y += (ch.height - lastHeight) / 2; // fix vertical position
}
had a go of this and my character kept falling through the terrain. But its ok ive made my own version using the SetVelocity function of the Character$$anonymous$$otor script.
I have seen other questions with people having trouble with the character collider falling through the terrain, I personally have never seen nor had this problem, and after 10 $$anonymous$$utes I am happy to say this is tested and working. Another question came up, so I did an answer : http://answers.unity3d.com/questions/374157/character-controller-slide-action-script.html
Great that you found a way yourself !
Thank you for all your help everyone! I actually found out a way to do it since I didn't think I was going to get this many answers. Although I do want to implement some of the functionality found in alucardj's script. Again, thank you!
Answer by Maulik2208 · Dec 27, 2012 at 01:15 PM
Why don't you make a trick rotate your player (Gameobject) and add some timer as well as extra force on the sliding (moving) direction.....all i want to say is that spacebar is pressed then player is rotated on some angle suitable to you and the timer starts for undoing rotation and take the player back to the original (standing position) and remove extra applied forces........Hope this idea works for you.....