Search Unity

Help with my c# code please!

Discussion in 'Scripting' started by samuraifan, Nov 24, 2015.

  1. samuraifan

    samuraifan

    Joined:
    Nov 24, 2015
    Posts:
    2
    Basically I have the player moving across the screen when the game is started. The plan is to create a basic game that uses basic controls (tap&hold to sprint and tap-tap to jump, may add a double jump) to complete a platform style game.

    I have a sprint countdown that triggered when the screen is pressed and held for longer than 1 second BUT in some instances the player will continue sprinting even though the screen is no longer pressed. It may be a minor problem but wasn't what I intended.

    What did I overlook?

    voidPlayerMover()
    {
    if(gameAlreadyStart ==true){
    Vector3 pos = transform.position;
    pos.x += moveSpeed *Time.deltaTime;
    transform.position = pos;
    anim.SetBool("running",true);
    }

    if(Input.GetMouseButton(0)){
    sprintCountDown -=Time.deltaTime;
    if(sprintCountDown <0){
    anim.SetBool("sprinting",true);
    }
    }

    if(Input.GetMouseButtonUp(0)){
    anim.SetBool("sprinting",false);
    sprintCountDown =1f;
    }

    if(Input.GetMouseButtonDown(0)){
    if(!one_click){
    one_click =true;
    Debug.Log("click");
    timer_for_double_click =Time.time;
    } else{
    one_click =false;
    Debug.Log("double click");
    if(onGround ==true)
    GetComponent<Rigidbody2D>().AddForce(newVector2(0f, playerJump));
    }​
    }

    if(one_click)
    {
    if((Time.time - timer_for_double_click)> jumpDelay){
    one_click =false;
    onGround =false;
    sprintCountDown =1f;
    }
    }​
    }
     
  2. Argonite

    Argonite

    Joined:
    Nov 1, 2015
    Posts:
    18
    Try putting

    "if(Input.GetMouseButtonUp(0))" after the codes for the sprinting. This means that when you release the tap, it will trigger an action, and you can put some codes to make it trigger by making the "sprint = false;".

    I'm not sure though, I'm just a beginner. Inform me if this works
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  4. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    try with else instead of buttonUp...
    and yes please do use code tags... :-(
    Code (CSharp):
    1. if(Input.GetMouseButton(0)){
    2. sprintCountDown -=Time.deltaTime;
    3. if(sprintCountDown <0){
    4. anim.SetBool("sprinting",true);
    5. }
    6. }
    7. else
    8. {
    9. anim.SetBool("sprinting",false);
    10. sprintCountDown =1f;
    11. }
     
  5. samuraifan

    samuraifan

    Joined:
    Nov 24, 2015
    Posts:
    2
    @Argonite Did what you suggested moreorless. I put the mouseup code in my Update function after the player movement function is called. Worked like a charm. Thanks :)
     
    Argonite likes this.
  6. Argonite

    Argonite

    Joined:
    Nov 1, 2015
    Posts:
    18
    No problem man. I am happy to help! Just like my post so others who need help can view it! ;)