I’m trying to make a script so that when I push the “S” key my character slides. So that he can slide under objects. But I have no idea where to begin. Is there a way to rotate the character controller capsule so that it is horizontal? That’s the only way I can see doing it. Thanks for reading!
-Rov
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
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class slide : MonoBehaviour
{
float walkSpeedt = 7; // regular speed
float crchSpeed = 3; // crouching speed
float runSpeed = 20; // run speed
private CharacterMotor chMotor;
private CharacterController ch;
private Transform tr;
private float height; // initial height
public float slideSpeed = 20; // slide speed
private bool isSliding = false;
private Vector3 slideForward; // direction of slide
private float slideTimer = 0.0f;
public float slideTimerMax = 2.5; // time while sliding
void Start()
{
chMotor = GetComponent(CharacterMotor);
tr = transform;
ch = GetComponent(CharacterController);
height = ch.height;
}
void Update()
{
float h = height;
float 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("LeftControl")) // press C to crouch
{
h = 0.5f * height;
speed = crchSpeed; // slow down when crouching
}
// - slide -
if (Input.GetKeyDown("LeftControl") && !isSliding) //crouch to slide
{
slideTimer = 0.0f; // start timer
isSliding = true;
slideForward = tr.forward;
}
if (isSliding)
{
h = 0.5f * 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
float 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
}
}
I think it should be it.