• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by isacoskun3 · Aug 01, 2017 at 08:12 AM · unity 5

can you help me please about my script ?

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;

using UnityEngine; using System.Collections;

public class FlappyScript : MonoBehaviour {

 public AudioClip FlyAudioClip, DeathAudioClip, ScoredAudioClip;
 public Sprite GetReadySprite;
 public float RotateUpSpeed = 1, RotateDownSpeed = 1;
 public GameObject IntroGUI, DeathGUI;
 public Collider2D restartButtonGameCollider;
 public float VelocityPerJump = 3;
 public float XSpeed = 1;

 // Use this for initialization
 void Start()
 {

 }

 FlappyYAxisTravelState flappyYAxisTravelState;

 enum FlappyYAxisTravelState
 {
     GoingUp, GoingDown
 }

 Vector3 birdRotation = Vector3.zero;
 // Update is called once per frame
 void Update()
 {
     //handle back key in Windows Phone
     if (Input.GetKeyDown(KeyCode.Escape))
         Application.Quit();

     if (GameStateManager.GameState == GameState.Intro)
     {
         MoveBirdOnXAxis();
         if (WasTouchedOrClicked())
         {
             BoostOnYAxis();
             GameStateManager.GameState = GameState.Playing;
             IntroGUI.SetActive(false);
             ScoreManagerScript.Score = 0;
         }
     }

     else if (GameStateManager.GameState == GameState.Playing)
     {
         MoveBirdOnXAxis();
         if (WasTouchedOrClicked())
         {
             BoostOnYAxis();
         }

     }

     else if (GameStateManager.GameState == GameState.Dead)
     {
         Vector2 contactPoint = Vector2.zero;

         if (Input.touchCount > 0)
             contactPoint = Input.touches[0].position;
         if (Input.GetMouseButtonDown(0))
             contactPoint = Input.mousePosition;

         //check if user wants to restart the game
         if (restartButtonGameCollider == Physics2D.OverlapPoint
             (Camera.main.ScreenToWorldPoint(contactPoint)))
         {
             GameStateManager.GameState = GameState.Intro;
             Application.LoadLevel(Application.loadedLevelName);
         }
     }

 }


 void FixedUpdate()
 {
     //just jump up and down on intro screen
     if (GameStateManager.GameState == GameState.Intro)
     {
         if (GetComponent<Rigidbody2D>().velocity.y < -1) //when the speed drops, give a boost
             GetComponent<Rigidbody2D>().AddForce(new Vector3(0, GetComponent<Rigidbody2D>().mass * 0 * Time.deltaTime)); //lots of play and stop 
                                                     //and play and stop etc to find this value, feel free to modify
     }
     else if (GameStateManager.GameState == GameState.Playing || GameStateManager.GameState == GameState.Dead)
     {
         FixFlappyRotation();
     }
 }

 bool WasTouchedOrClicked()
 {
     if (Input.GetButtonUp("Jump") || Input.GetMouseButtonDown(0) || 
         (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Ended))
         return true;
     else
         return false;
 }

 void MoveBirdOnXAxis()
 {
     transform.position += new Vector3(Time.deltaTime * XSpeed, 0, 0);
 }

 void BoostOnYAxis()
 {
     GetComponent<Rigidbody2D>().velocity = new Vector2(0, VelocityPerJump);
     GetComponent<AudioSource>().PlayOneShot(FlyAudioClip);
 }



 /// <summary>
 /// when the flappy goes up, it'll rotate up to 45 degrees. when it falls, rotation will be -90 degrees min
 /// </summary>
 private void FixFlappyRotation()
 {
     if (GetComponent<Rigidbody2D>().velocity.y > 0) flappyYAxisTravelState = FlappyYAxisTravelState.GoingUp;
     else flappyYAxisTravelState = FlappyYAxisTravelState.GoingDown;

     float degreesToAdd = 0;

     switch (flappyYAxisTravelState)
     {
         case FlappyYAxisTravelState.GoingUp:
             degreesToAdd = 6 * RotateUpSpeed;
             break;
         case FlappyYAxisTravelState.GoingDown:
             degreesToAdd = -3 * RotateDownSpeed;
             break;
         default:
             break;
     }
     //solution with negative eulerAngles found here: 

     //clamp the values so that -90<rotation<45 *always*
     birdRotation = new Vector3(0, 0, Mathf.Clamp(birdRotation.z + degreesToAdd, -90, 45));
     transform.eulerAngles = birdRotation;
 }

 /// <summary>
 /// check for collision with pipes
 /// </summary>
 /// <param name="col"></param>
 void OnTriggerEnter2D(Collider2D col)
 {
     if (GameStateManager.GameState == GameState.Playing)
     {
         if (col.gameObject.tag == "Pipeblank") //pipeblank is an empty gameobject with a collider between the two pipes
         {
             GetComponent<AudioSource>().PlayOneShot(ScoredAudioClip);
             ScoreManagerScript.Score++;
         }
         else if (col.gameObject.tag == "Pipe")
         {
             FlappyDies();
         }
     }
 }

 void OnCollisionEnter2D(Collision2D col)
 {
     if (GameStateManager.GameState == GameState.Playing)
     {
         if (col.gameObject.tag == "Floor")
         {
             FlappyDies();
         }
     }
 }

 void FlappyDies()
 {
     GameStateManager.GameState = GameState.Dead;
     DeathGUI.SetActive(true);
     GetComponent<AudioSource>().PlayOneShot(DeathAudioClip);
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

172 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Varialbes on prefab script always null 0 Answers

Trying to move buttons via Coroutine and transform.Translate 0 Answers

CANT BUILD HELP WITH ERRORS? 0 Answers

Unity Freezing 1 Answer

Storing data from previous shader pass 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges