- Home /
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
I personally think a slide is a crouch at a run speed with none or very limited left-right movement. From my comment on a similar question : http://answers.unity3d.com/questions/368509/how-to-do-sliding-like-crysis-andor-farcry.html
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
I'm having a bit of trouble. I have this script here,
function Update(){
var vScale : 1.0;
if (Input.Get$$anonymous$$ey("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
Answer by AlucardJay · Jan 05, 2013 at 04:02 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
}
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 $$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/368509/how-to-do-sliding-like-crysis-andor-farcry.html
Great that you found a way yourself !
This thread is REALLY old, but I still have one question: I don't have a "Character$$anonymous$$oter" 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...
This is the old uJS aka 'UnityScript', Unity javascript, and worked with the Character Controller component back in Unity 3.5 ~ 4. the way variables are declared and typecast is different, e.g. :
// uJS
var variableName : VariableType;
// C#
VariableType variableName;
There was also another language before Unity5, called Boo
oh thank you! so i need to convert this?
Answer by mardusmordos · Oct 16, 2021 at 09:42 AM
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.