- Home /
Rigidbody controller slide up walls?(Sidescroller)
Hello! I need some help with rigidbody sidescroller script!
I want to make a really 'smooth' sidescroller controller based on rigidbody. By 'smooth' I mean a controller that relies a lot on physics and can be easily be influenced by other objects in the scene.
Now the problem I'm having at the moment is that I don't know how to make it so that my character would slide on walls when I jump into them. At the moment when I jump into a wall and I keep pressing horizontal movement my character just gets 'stuck' and doesn't move and when I release horizontal controls it takes about half a seconds before the character starts falling.
So what I want to do is that when I jump into a wall the character wouldn't just 'stop' but would keep sliding on the wall up or down depending on the angel it hits the wall. I guess the best example would be something similar to Super Meat Boy but without locking character to the wall.
Here's the script I managed to put together:
#pragma strict
var MoveSpeed : float = 1;
var JumpSpeed : float = 1;
private var rb: Rigidbody;
function Start () {
rb = GetComponent.<Rigidbody>();
}
function FixedUpdate () {
var targetVelocity = new Vector3(Input.GetAxis("Horizontal"), 0, 0);
targetVelocity = transform.TransformDirection(targetVelocity)*MoveSpeed;
var velocity = rb.velocity;
var velocityChange = (targetVelocity - velocity);
velocityChange.y = 0;
rb.AddForce(velocityChange, ForceMode.VelocityChange);
if (Input.GetButtonDown("Jump")) {
rb.velocity = Vector3(0,JumpSpeed,0);
}
}
Answer by Pendantic · Mar 13, 2015 at 05:57 AM
This isn't a perfect solution but if you make a physics material and make it's friction 0 then attach it to the player this stops all stickiness. If you do however want some friction for the ground, you can make another collider for the ground and just position this at the feet of the character. I've done this a lot and it's worked really well.
This solution actually works more for me at this point of the development. Simple, easy, physics based- everything I was looking for. This provably works better when you are developing a physics based game. Also I feel like this solutions causes less bugs down the road too.
Thanks, finally the last piece I needed to find the CharacterController behavior back with a rigidbody approach. Finally an acceptable custom character controller!
Answer by Mubanga · Mar 13, 2015 at 01:37 AM
You can add a trigger or a raycast to you character that checks just a lil bit in front of your collider. If that trigger finds something you can slide against, you disable movement in the direction of the wall and you can even slow down your decent if you wish.
I thought about it too but solution like this would be REALLY sloppy, the whole idea of it is to be really smooth. I need the character to slide up depending on the angel it hits the wall. If I jump into the wall really close it should slide up until it has used up all of it's 'kinetic energy' and then it would fall down like in real world.
if you stop velocity in the x direction before your collider hits the wall you should get exactly that. I had the same problem a while ago. I will see if I can find my code for that project.
I might try something like that then just see if it works and how well it works, using raycasts is provably the best way of doing that. But I still need to have air control after I hit the wall too so I'm not sure how to disable velocity X in only one X direction when using GetAxis("Horizontal"). I might need to make the script longer by defining left and right individually.
GetAxis("Horizontal") gives a float between -1 (left) and 1 (right) so to disable movement to the right you do if (hAxis > 0) hAxis = 0;
Ok I guess that answers that question. Definitely will try that then in few hours to see if it works.