- Home /
How to handle walls with my FirstPersonCharacter?
I am using a rigidbody to control my character. I am setting its velocity based an the player input, and it works pretty great (it's similiar to the one in the new Sample Assets from Unity). Only problem is: If my character jumps against a wall and keeps moving in the direction of the wall, the character isn't falling. Any ideas how I could solve this?
We will need to see some code please. Hard to know how you are implementing your character controller exactly. No code, no context.
I use a state machine to handle movement, but I wrote it as a c# script to show what it does:
public class Pseudo$$anonymous$$ove : $$anonymous$$onoBehaviour {
Vector3 input;
float verticalVelocity = 0.0f;
float speed = 10.0f;
float jumpStrenght = 8f;
// Update is called once per frame
void FixedUpdate () {
input = new Vector3(Input.GetAxis("Horizontal")*speed,0,Input.GetAxis("Vertical")*speed);
input = transform.TransformDirection(input);
verticalVelocity = rigidbody.velocity.y;
if(Input.GetButtonDown("Jump"))
{
verticalVelocity += jumpStrenght;
}
rigidbody.velocity = input + Vector3.up * verticalVelocity;
rigidbody.AddForce(0f,-9.81f,0f);
}
}
I left ground checking out, so it doesnt distract. $$anonymous$$y main problem is walls and ledges, where the character just keeps getting stuck until moved in another direction.
Woooooow I am an idiot. The problem was friction on the physics material that let the charcter getting stuck on things. I'm going to go cry now... Thanks anyway!
Answer by Nanobrain · Feb 04, 2014 at 06:32 PM
Since your character is a ridgidbody, why not just let the built in code handle the fall for you. Set your ridgidbody to be effected by gravity in the editor. Then when
if(Input.GetButtonDown("Jump"))
{
ridgidBody.AddForce(0f, jumpSpeed, 0f);
}
Then remove this
rigidbody.velocity = input + Vector3.up * verticalVelocity;
rigidbody.AddForce(0f,-9.81f,0f);