Search Unity

I NEED TO HELP ABOUT MY SCRIPT

Discussion in 'Scripting' started by isacoskun3, Jul 25, 2017.

  1. isacoskun3

    isacoskun3

    Joined:
    Jul 25, 2017
    Posts:
    1
    hello everybody. I edit a game but have a problem. when i touch screen my character jumps two times and plays two times jump sound.

    Why is this caused ? I give a command once, but I do it twice

    my script;


    Code (CSharp):
    1. public AudioClip FlyAudioClip, DeathAudioClip, ScoredAudioClip;
    2. public Sprite GetReadySprite;
    3. public float RotateUpSpeed = 1, RotateDownSpeed = 1;
    4. public GameObject IntroGUI, DeathGUI;
    5. public Collider2D restartButtonGameCollider;
    6. public float VelocityPerJump = 3;
    7. public float XSpeed = 1;
    8. // Use this for initialization
    9. void Start()
    10. {
    11. }
    12. FlappyYAxisTravelState flappyYAxisTravelState;
    13. enum FlappyYAxisTravelState
    14. {
    15.      GoingUp, GoingDown
    16. }
    17. Vector3 birdRotation = Vector3.zero;
    18. // Update is called once per frame
    19. void Update()
    20. {
    21.      //handle back key in Windows Phone
    22.      if (Input.GetKeyDown(KeyCode.Escape))
    23.          Application.Quit();
    24.      if (GameStateManager.GameState == GameState.Intro)
    25.      {
    26.          MoveBirdOnXAxis();
    27.          if (WasTouchedOrClicked())
    28.          {
    29.              BoostOnYAxis();
    30.              GameStateManager.GameState = GameState.Playing;
    31.              IntroGUI.SetActive(false);
    32.              ScoreManagerScript.Score = 0;
    33.          }
    34.      }
    35.      else if (GameStateManager.GameState == GameState.Playing)
    36.      {
    37.          MoveBirdOnXAxis();
    38.          if (WasTouchedOrClicked())
    39.          {
    40.              BoostOnYAxis();
    41.          }
    42.      }
    43.      else if (GameStateManager.GameState == GameState.Dead)
    44.      {
    45.          Vector2 contactPoint = Vector2.zero;
    46.          if (Input.touchCount > 0)
    47.              contactPoint = Input.touches[0].position;
    48.          if (Input.GetMouseButtonDown(0))
    49.              contactPoint = Input.mousePosition;
    50.          //check if user wants to restart the game
    51.          if (restartButtonGameCollider == Physics2D.OverlapPoint
    52.              (Camera.main.ScreenToWorldPoint(contactPoint)))
    53.          {
    54.              GameStateManager.GameState = GameState.Intro;
    55.              Application.LoadLevel(Application.loadedLevelName);
    56.          }
    57.      }
    58. }
    59. void FixedUpdate()
    60. {
    61.      //just jump up and down on intro screen
    62.      if (GameStateManager.GameState == GameState.Intro)
    63.      {
    64.          if (GetComponent<Rigidbody2D>().velocity.y < -1) //when the speed drops, give a boost
    65.              GetComponent<Rigidbody2D>().AddForce(new Vector3(0, GetComponent<Rigidbody2D>().mass * 0 * Time.deltaTime)); //lots of play and stop
    66.                                                      //and play and stop etc to find this value, feel free to modify
    67.      }
    68.      else if (GameStateManager.GameState == GameState.Playing || GameStateManager.GameState == GameState.Dead)
    69.      {
    70.          FixFlappyRotation();
    71.      }
    72. }
    73. bool WasTouchedOrClicked()
    74. {
    75.      if (Input.GetButtonUp("Jump") || Input.GetMouseButtonDown(0) ||
    76.          (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Ended))
    77.          return true;
    78.      else
    79.          return false;
    80. }
    81. void MoveBirdOnXAxis()
    82. {
    83.      transform.position += new Vector3(Time.deltaTime * XSpeed, 0, 0);
    84. }
    85. void BoostOnYAxis()
    86. {
    87.      GetComponent<Rigidbody2D>().velocity = new Vector2(0, VelocityPerJump);
    88.      GetComponent<AudioSource>().PlayOneShot(FlyAudioClip);
    89. }
    90. /// <summary>
    91. /// when the flappy goes up, it'll rotate up to 45 degrees. when it falls, rotation will be -90 degrees min
    92. /// </summary>
    93. private void FixFlappyRotation()
    94. {
    95.      if (GetComponent<Rigidbody2D>().velocity.y > 0) flappyYAxisTravelState = FlappyYAxisTravelState.GoingUp;
    96.      else flappyYAxisTravelState = FlappyYAxisTravelState.GoingDown;
    97.      float degreesToAdd = 0;
    98.      switch (flappyYAxisTravelState)
    99.      {
    100.          case FlappyYAxisTravelState.GoingUp:
    101.              degreesToAdd = 6 * RotateUpSpeed;
    102.              break;
    103.          case FlappyYAxisTravelState.GoingDown:
    104.              degreesToAdd = -3 * RotateDownSpeed;
    105.              break;
    106.          default:
    107.              break;
    108.      }
    109.      //solution with negative eulerAngles found here:
    110.      //clamp the values so that -90<rotation<45 *always*
    111.      birdRotation = new Vector3(0, 0, Mathf.Clamp(birdRotation.z + degreesToAdd, -90, 45));
    112.      transform.eulerAngles = birdRotation;
    113. }
    114. /// <summary>
    115. /// check for collision with pipes
    116. /// </summary>
    117. /// <param name="col"></param>
    118. void OnTriggerEnter2D(Collider2D col)
    119. {
    120.      if (GameStateManager.GameState == GameState.Playing)
    121.      {
    122.          if (col.gameObject.tag == "Pipeblank") //pipeblank is an empty gameobject with a collider between the two pipes
    123.          {
    124.              GetComponent<AudioSource>().PlayOneShot(ScoredAudioClip);
    125.              ScoreManagerScript.Score++;
    126.          }
    127.          else if (col.gameObject.tag == "Pipe")
    128.          {
    129.              FlappyDies();
    130.          }
    131.      }
    132. }
    133. void OnCollisionEnter2D(Collision2D col)
    134. {
    135.      if (GameStateManager.GameState == GameState.Playing)
    136.      {
    137.          if (col.gameObject.tag == "Floor")
    138.          {
    139.              FlappyDies();
    140.          }
    141.      }
    142. }
    143. void FlappyDies()
    144. {
    145.      GameStateManager.GameState = GameState.Dead;
    146.      DeathGUI.SetActive(true);
    147.      GetComponent<AudioSource>().PlayOneShot(DeathAudioClip);
    148. }
    149.  
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    My best guess is you still have Input.simulateMouseWithTouches set to True (default value). If so I believe in WasTouchedOrClicked() when you begin a touch, Input.GetMouseButtonDown(0) would be True, which causes the first jump. Then when you release your touch you get TouchPhase.Ended which triggers the 2nd jump.

    I haven't done much with touch interfaces, so I'm not 100% sure if it works as I described. Try setting Input.simulateMouseWithTouches to False or temporarily remove your check for mouse clicks in WasTouchedOrClicked() to further debug.

    Also, your thread title is terrible, being that it doesn't say anything about the kind of issue you're having. I'm actually surprised I even clicked the thread, but I was bored and curious.