Character Controller Slide Action Script!

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

Thanks allot I'll check those links out! -Rov

I'm having a bit of trouble. I have this script here, function Update(){ var vScale : 1.0; if (Input.GetKey("c")){ vScale = 0.5; } } But when I apply it to my character nothing happens. What should I do? I don't know exactly what's going on. Thanks you so much though! This is farther then I've gotten before! -Rov

What is vScale? It seems that there is a missing part of your code.

vScale is what I got from the links posted by alucardj. It's supposed to be the scale of the character controller capsule. So according to the script when the "c" button is pushed the capsule should halve. -Rov

I have been playing around with this, and may have an answer (Jay Kay)

2 Answers

2

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
}

Thanks for all the help! I found another script here that fits into what I'm doing really well. Thanks for the help though! http://forum.unity3d.com/threads/48187-scale-character-controller-but-not-player For some reason though, my character likes to fall through the floor.=/ -Rov

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 minutes I am happy to say this is tested and working. Another question came up, so I did an answer : http://answers.unity3d.com/questions/368509/how-to-do-sliding-like-crysis-andor-farcry.html Great that you found a way yourself !

thx so much

This thread is REALLY old, but I still have one question: I don't have a "CharacterMoter" on my Player, so how would I lock my Player to only move forward?

this code is giving me so many compile errors! what language is this in? nothings seems right or where it's supposed to be...

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.