So the idea of the code is, when i press “Space” and the object is hit on the right or the left by an object with tag “Wall” to be pushed in the oposite direction. But when it is hit on the left or right side the booleans don’t register that.
var sidePush : float = 4f;
var upPush : float = 2f;
var leftSide : boolean = false;
var rightSide : boolean = false;
var jumpKey : KeyCode;
function Update () {
Debug.DrawLine(Vector3(transform.position.x - 0.5, transform.position.y, transform.position.z), Vector3(transform.position.x - 1.5, transform.position.y, transform.position.z), Color.red);
Debug.DrawLine(Vector3(transform.position.x + 0.5, transform.position.y, transform.position.z), Vector3(transform.position.x + 1.5, transform.position.y, transform.position.z), Color.blue);
//Push to the right
if(Input.GetKeyDown(jumpKey) && leftSide == true)
{
GetComponent.<Rigidbody>().velocity.x = sidePush;
GetComponent.<Rigidbody>().velocity.y = upPush;
leftSide = false;
}
//Push to the left
if(Input.GetKeyDown(jumpKey) && rightSide == true)
{
GetComponent.<Rigidbody>().velocity.x = -sidePush;
GetComponent.<Rigidbody>().velocity.y = upPush;
rightSide = false;
}
}
function OnCollisionEnter (coli : Collision) {
var hitRight : RaycastHit2D = Physics2D.Linecast(Vector2(transform.position.x + 0.5,transform.position.y), Vector2(transform.position.x + 1.5,transform.position.y));
var hitLeft : RaycastHit2D = Physics2D.Linecast(Vector2(transform.position.x - 0.5,transform.position.y), Vector2(transform.position.x - 1.5,transform.position.y));
//THE PROBLEM IS HERE SOMEWHERE!!!!!
if(hitRight.collider != null && coli.gameObject.tag == "Wall")
{
rightSide = true;
}
else if(hitLeft.collider != null && coli.gameObject.tag == "Wall")
{
leftSide = true;
}
}
function OnCollisionExit ()
{
rightSide = false;
leftSide = false;
}