Monday, April 3, 2017

physics - 2D character controller in unity (trying to get old-school platformers back)


This days I'm trying to create a 2D character controller with unity (using phisics). I'm fairly new to physic engines and it is really hard to get the control feel I'm looking for. I would be really happy if anyone could suggest solution for a problem I'm finding:


This is my FixedUpdate right now:



public void FixedUpdate()
{
Vector3 v=new Vector3(0,-10000*Time.fixedDeltaTime,0);
_body.AddForce(v);

v.y=0;
if(state(MovementState.Left))
{
v.x=-_walkSpeed*Time.fixedDeltaTime+v.x;
if(Mathf.Abs(v.x)>_maxWalkSpeed) v.x=-_maxWalkSpeed;

}
else if(state(MovementState.Right))
{
v.x= _walkSpeed*Time.fixedDeltaTime+v.x;
if(Mathf.Abs(v.x)>_maxWalkSpeed) v.x=_maxWalkSpeed;
}

_body.velocity=v;

Debug.Log("Velocity: "+_body.velocity);

}

I'm trying here to just move the rigid body applying a gravity and a linear force for left and right. I have setup a physic material that makes no bouncing and 0 friction when moving and 1 friction with stand still. The main problem is that I have colliders with slopes and the velocity changes from going up (slower) , going down the slope (faster) and walk on a straight collider (normal). How could this be fixed? As you see I'm applying allways same velocity for x axis.


For the player I have it setup with a sphere at feet position that is the rigidbody I'm applying forces to.


Any other tip that could make my life easier with this are welcomed :).


P.D. While coming home I have noticed I could solve this applying a constant force parallel to the surface the player is walking, but don't know if it is best method.




No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...