Search Unity

Why doesn't it register?

Discussion in 'Scripting' started by Bloodlister, Mar 29, 2015.

  1. Bloodlister

    Bloodlister

    Joined:
    May 18, 2014
    Posts:
    6
    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.

    Code (JavaScript):
    1.  
    2. var sidePush : float = 4f;
    3. var upPush : float = 2f;
    4. var leftSide : boolean = false;
    5. var rightSide : boolean = false;
    6. var jumpKey : KeyCode;
    7.  
    8. function Update () {
    9.     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);
    10.     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);
    11.    
    12. //Push to the right
    13.     if(Input.GetKeyDown(jumpKey) && leftSide == true)
    14.     {
    15.         GetComponent.<Rigidbody>().velocity.x = sidePush;
    16.         GetComponent.<Rigidbody>().velocity.y = upPush;
    17.         leftSide = false;
    18.     }
    19. //Push to the left
    20.     if(Input.GetKeyDown(jumpKey) && rightSide == true)
    21.     {
    22.         GetComponent.<Rigidbody>().velocity.x = -sidePush;
    23.         GetComponent.<Rigidbody>().velocity.y = upPush;
    24.         rightSide = false;
    25.     }
    26. }
    27.  
    28. function OnCollisionEnter (coli : Collision) {
    29.     var hitRight : RaycastHit2D = Physics2D.Linecast(Vector2(transform.position.x + 0.5,transform.position.y), Vector2(transform.position.x + 1.5,transform.position.y));
    30.     var hitLeft : RaycastHit2D = Physics2D.Linecast(Vector2(transform.position.x - 0.5,transform.position.y), Vector2(transform.position.x - 1.5,transform.position.y));
    31.  
    32.     //THE PROBLEM IS HERE SOMEWHERE!!!!!
    33.     if(hitRight.collider != null && coli.gameObject.tag == "Wall")
    34.     {
    35.         rightSide = true;
    36.     }
    37.    
    38.     else if(hitLeft.collider != null && coli.gameObject.tag == "Wall")
    39.     {
    40.         leftSide = true;
    41.     }
    42. }
    43.  
    44. function OnCollisionExit ()
    45. {
    46.     rightSide = false;
    47.     leftSide = false;
    48. }
     
  2. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899
    You are using OnCollisionEnter which is 3D and then you use Physics2D.Linecast which is 2D. Are you really mixing 2D and 3D physics or should you be using OnCollisionEnter2D?
     
  3. Bloodlister

    Bloodlister

    Joined:
    May 18, 2014
    Posts:
    6
    When i try to use this
    Code (JavaScript):
    1. function OnCollisionEnter (coli : Collision) {
    2.     var hitRight : RaycastHit = Physics.Linecast(Vector3(transform.position.x + 0.5,transform.position.y,0), Vector3(transform.position.x + 1.5,transform.position.y,0));
    3.     var hitLeft : RaycastHit = Physics.Linecast(Vector3(transform.position.x - 0.5,transform.position.y,0), Vector3(transform.position.x - 1.5,transform.position.y,0));
    4.    
    5.     if(hitRight.collider != null && coli.gameObject.tag == "Wall")
    6.     {
    7.         rightSide = true;
    8.         Debug.Log("You hit: " + hitRight.transform);
    9.     }
    10.    
    11.     else if(hitLeft.collider != null && coli.gameObject.tag == "Wall")
    12.     {
    13.         leftSide = true;
    14.         Debug.Log("You hit: " + hitLeft.transform);
    15.     }
    16. }
    It gives a error saying "Cannot convert "boolean" to "UnityEngine.RaycastHit""
     
  4. PGJ

    PGJ

    Joined:
    Jan 21, 2014
    Posts:
    899