- Home /
Character controller slide
Hello, i'm really sorry if this have already get an answer, but i'm stuck since one week, and I'am so done about it.
Here is my problem : I have a runner where my character controller move in Z. My character can also dodge by swaping on X (left and right)
My swap function is actually working pretty good. But, it's ugly as fuck. On my actual code, my character is just teleported to a place. I would like him to slide to the left and to the right.
The problem is that on the unity tutorial this function work only when the player keep the key pressed. I would like to see my character slide to a specific point when the player press the key only once.
here is my code :
void deplacements()
{
moveVector = Vector3.zero;
if (controller.isGrounded) {
verticalVelocity = -0.5f;
}
else {
verticalVelocity -= gravity * Time.deltaTime;
}
//x -Left and Right
if(Input.GetKeyDown(KeyCode.LeftArrow) && controller.isGrounded){
if(swap < 1){
//transform.Translate (-3, 0, 0);
swap += 1;
}
}
if(Input.GetKeyDown(KeyCode.RightArrow) && controller.isGrounded){
if (swap > -1) {
//transform.Translate (3, 0, 0);
swap += -1;
}
}
//y -Up and Down
if (Input.GetKeyDown(KeyCode.Space)) {
if (inteligent) {
if (controller.isGrounded) {
//transform.position += new Vector3 (0, 2, 0);
verticalVelocity = 10;
//controller.velocity = new Vector3(0,10,0);
}
}
}
moveVector.y = verticalVelocity;
//z -Forward and Backward
if (canBeHit == false)
{
moveVector.z = -speed;
} else
{
moveVector.z = speed;
}
//moveVector.z = speed;
controller.Move ((moveVector)*Time.deltaTime);
}
void constraintSwap()
{
if (swap == 1)
transform.position = new Vector3 (-3, transform.position.y, transform.position.z);
if (swap == -1)
transform.position = new Vector3 (3, transform.position.y, transform.position.z);
if (swap == 0)
transform.position = new Vector3 (0, transform.position.y, transform.position.z);
}
Your answer

Follow this Question
Related Questions
Character Controller 1 Answer
Force on Character Controller (Knockback) 4 Answers
rotate character in 2.5D 1 Answer
What is wrong with my CharController script? 1 Answer
Character Controller teleporting to ground instead of falling 1 Answer