Hi guys.
I’m the developer of a small team currently working on a 2D gameplay platformer using unity. I’m currently testing a physic based alternative to the transform.position based code I’ve been using up until now. I’ve obtained some satisfying results but a few things still compromise the usability of the method. So I came here searching for people with more experience than me in the use of physics.
I’m using a simple capsule collider as character and I move it by using AddForce() until the maximum velocity is reached. I make it jump by adding a large vertical positive force when the Jump button is presed. The Spiderman problem is that if I jump near a wall while moving toward it, the wall slow down the jump and we fail to reach the top of the wall, worst, we stay glued to the wall if we keep the key presed.
I’ve tried the following solutions:
- Using physics materials with no friction: no difference.
- Adding Bounciness: no difference on the side and awful result on the top (kangaroo style).
- Forbidding the addforce if a sweeptest detect an obstacle (ignoring collision with the floor): partial result.
private var Jump = false;
private var Grounded = false;
private var hit : RaycastHit;
private var VitesseMax : float = 0;
Physics.gravity = Vector3(0, -30.0, 0);
function FixedUpdate ()
{
//Running
if(Input.GetButton("Fire1") == true){VitesseMax = 10;}else{VitesseMax = 5;}
//Moving forward if there is no obstacle.
if (!rigidbody.SweepTest (Vector3(Mathf.Sign(Input.GetAxis("Horizontal"))*1,0,0), hit, 0.1))
{
if(rigidbody.velocity.x < VitesseMax rigidbody.velocity.x > -VitesseMax)
{
rigidbody.AddForce (Input.GetAxis("Horizontal")*30,0,0);
}
}
else
{
if(hit.point.y < transform.position.y - ((collider.height/2)*0.9) Grounded == true)
{
if(rigidbody.velocity.x < VitesseMax rigidbody.velocity.x > -VitesseMax)
{
rigidbody.AddForce (Input.GetAxis("Horizontal")*30,0,0);
}
}
else
{
rigidbody.velocity.x = 0;
}
}
//Jumping
if(Input.GetButton("Jump") == true Grounded == true){Jump = true;}
if(Input.GetButton("Jump") == false Jump == true)
{
rigidbody.AddForce (0,400,0);
Grounded = false;
Jump = false;
}
transform.position.z = 0;
}
function OnCollisionEnter(collision : Collision)
{
if(collision.contacts[0].point.x <= transform.position.x + collider.radius collision.contacts[0].point.x >= transform.position.x - collider.radius collision.contacts[0].normal.y > 0)
{
Grounded = true;
}
}
This last solution is efficient with floating nonmoving platform, we can’t Spiderman on them. We can’t Spiderman on wall either but the jump reduction is still here. Finally, we can still Spiderman on moving platform while they move toward us.
If you guys have ideas on the way I can modify my tests or methods to achieve the desired result it would help me greatly.