Unity 2D Platform pixel perfect physics don't work?
My two solutions (located at 33 and 37) seem to work fine while going left against a wall (the first one) but it seems it does not push the player left while going right (resulting in them being stuck.) Should I got even attempt to combine rigidbodies and these (seemingly pixel perfect) physics calculations? How the hell do you do pixel calculations with floats???
Any help is appreciated
public class PlayerController : MonoBehaviour {
private Rigidbody2D body;
public float speed = 10f;
public float jumpPower = 5000f;
private bool grounded;
private Vector2 groundCheckPos;
// Use this for initialization
void Start () {
body = GetComponent<Rigidbody2D> ();
}
public bool isGrounded(){
bool result = Physics2D.Linecast(transform.position , groundCheckPos , 1 << LayerMask.NameToLayer("Ground"));
if (result) {
Debug.DrawLine(transform.position , groundCheckPos , Color.green, 0.5f, false);
}
else {
Debug.DrawLine(transform.position , groundCheckPos , Color.red, 0.5f, false);
}
return result;
}
// Update is called once per frame
void FixedUpdate () {
float h = Input.GetAxisRaw ("Horizontal");
while (Physics2D.Linecast (transform.position, new Vector2 (transform.position.x - ((1f / 16f) * 3f), transform.position.y), 1 << LayerMask.NameToLayer ("Ground"))) {
transform.position = new Vector2(transform.position.x + (1f / 16f), transform.position.y);
}
while (Physics2D.Linecast (transform.position, new Vector2 (transform.position.x + ((1f / 16f) * 3f), transform.position.y), 1 << LayerMask.NameToLayer ("Ground"))) {
transform.position = new Vector2(transform.position.x - (1f / 16f), transform.position.y);
}
transform.position = new Vector2(transform.position.x + (speed * h * Time.deltaTime), transform.position.y);
float v = Input.GetAxisRaw ("Vertical");
groundCheckPos = new Vector2 (transform.position.x, transform.position.y - 1.05f);
if (isGrounded ()) {
body.AddForce ((Vector2.up * jumpPower) * v);
}
}
void Update() {
}
}
Comment