Search Unity

Moving left and right not working as expected. Please help

Discussion in '2D' started by mcarthur03, Aug 14, 2016.

  1. mcarthur03

    mcarthur03

    Joined:
    Jan 29, 2015
    Posts:
    26
    Ok... Ive been working on getting this working for about 2 hours now, and it is as far as im concerned its working entirely other than ONE thing...

    The problem, is that, when my player is in the air and they move right and collide with a wall they simply collide. Simple.

    But when the player is moving left and more commonly left and down, or left straight after falling off a ledge. And hitting a wall, they will almost always, for some reason, become grounded.

    What is confusing me is that the left and right move scripts are exactly the same, with a few variables changed so that they are designed to go in opposite directions. My grounded check is also, centered, so i dont imagine it could be happening to left due to it being offset.

    If anyone could help i would greatly appreciate it! Here is the script: (sorry but unity wont let me make a code box for some reason :p)

    using UnityEngine; using System.Collections;

    public class Move : MonoBehaviour {

    publicRaycastHit2D groundhit;

    publicfloat groundeddistance;publicbool grounded;publicfloat fallspeed;

    publicRaycastHit2D uphit;publicbool upblocked;publicfloat upspeed;publicfloat jumpcount;

    publicfloat fallspeedpubset;

    publicbool leftblock;publicbool rightblock;publicfloat sidespeed;publicRaycastHit2D lefthit;publicRaycastHit2D righthit;publicfloat leftreach;publicfloat rightreach;

    publicbool jumping;

    publicbool gravity;publicfloat groundedwidth;publicfloat upreach;publicfloat jumpcountmax;publicfloat repeatratejump;


    publicbool cantground;//public bool canupspeed;//public bool jumping;//public float upincreaserate;//public bool candownspeed;

    publicfloat leftclimbcount;publicfloat rightclimbcount;publicfloat leftclimbcountstoper;publicfloat rightclimbcountstoper;


    // Use this for initializationvoidStart(){

    }

    // Update is called once per framevoidUpdate(){

    fallspeed = fallspeedpubset;//-//=-


    if(cantground ==false){
    groundhit =Physics2D.BoxCast(transform.position,newVector2(transform.localScale.x - groundedwidth, transform.localScale.y), transform.eulerAngles.z,Vector2.down, groundeddistance);if(Physics2D.BoxCast(transform.position,newVector2(transform.localScale.x - groundedwidth, transform.localScale.y), transform.eulerAngles.z,Vector2.down, groundeddistance)&& groundhit.transform.tag =="Terrain"){
    grounded =true;}else{ grounded =false;}


    }

    if(grounded ==false&& gravity){ transform.position =newVector2(transform.position.x, transform.position.y - fallspeed);}else{}


    lefthit =Physics2D.BoxCast(newVector2(transform.position.x - transform.localScale.x /2, transform.position.y),newVector2(0.01f, transform.localScale.y -0.04f), transform.eulerAngles.z,Vector2.left, leftreach);if(Physics2D.BoxCast(newVector2(transform.position.x - transform.localScale.x /2, transform.position.y),newVector2(0.01f, transform.localScale.y -0.04f), transform.eulerAngles.z,Vector2.left, leftreach)&& lefthit.transform.tag =="Terrain"){
    leftblock =true;}else{ leftblock =false;}

    righthit =Physics2D.BoxCast(newVector2(transform.position.x + transform.localScale.x /2, transform.position.y),newVector2(0.01f, transform.localScale.y -0.04f), transform.eulerAngles.z,Vector2.right, rightreach);if(Physics2D.BoxCast(newVector2(transform.position.x + transform.localScale.x /2, transform.position.y),newVector2(0.01f, transform.localScale.y -0.04f), transform.eulerAngles.z,Vector2.right, rightreach)&& righthit.transform.tag =="Terrain"){
    rightblock =true;}else{ rightblock =false;}

    uphit =Physics2D.BoxCast(newVector2(transform.position.x,(transform.position.y + transform.localScale.y /2)+0.01f),newVector2(transform.localScale.x - groundedwidth,0.01f), transform.eulerAngles.z,Vector2.up, upreach);if(Physics2D.BoxCast(newVector2(transform.position.x,(transform.position.y + transform.localScale.y /2)+0.01f),newVector2(transform.localScale.x - groundedwidth,0.01f), transform.eulerAngles.z,Vector2.up, upreach)&& uphit.transform.tag =="Terrain"){
    upblocked =true;}else{ upblocked =false;}

    if(Input.GetKey("d")&& rightblock ==false){
    transform.position =newVector2(transform.position.x - sidespeed, transform.position.y);}elseif(Input.GetKey("a")&& leftblock ==false){
    transform.position =newVector2(transform.position.x + sidespeed, transform.position.y);}

    //if(grounded && Input.GetButtonDown("Jump")) { grounded = false; cantground = true; jumping = true; }if(grounded &&Input.GetKeyDown("w")&& upblocked ==false){InvokeRepeating("Jump",0, repeatratejump);}if(grounded){ rightclimbcount =0; leftclimbcount =0;}//if (grounded == false && lefthit && Input.GetKey("a") && leftclimbcount <= leftclimbcountstoper && Input.GetKey("w") == false) { gravity = false; leftclimbcount = leftclimbcount + 1; upblocked = true; } else if(jumping == false) { gravity = true; }//if (grounded == false && righthit && Input.GetKey("d") && rightclimbcount <= rightclimbcountstoper && Input.GetKey("w") == false) { gravity = false; rightclimbcount = rightclimbcount + 1; upblocked = true; } else if (jumping == false) { gravity = true; }

    }voidJump(){
    jumping =true;
    gravity =false;
    fallspeed =0;if(upblocked ==false)
    transform.position =newVector2(transform.position.x, transform.position.y + upspeed);
    jumpcount +=1;if(jumpcount >= jumpcountmax){CancelInvoke(); gravity =true; jumpcount =0; jumping =false;}

    }
    }