Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by aliens9889 · Sep 27, 2016 at 06:05 AM · 2d2d game2d-gameplay

How could I disable a click on a sprite when player is moving?

Hi everybody!!! I'm working in a mini game and the objective is try to get out of the kitchen. The player can walk through the room by clicking the floor that you want to go, and if you do that the Energy "Energia" goes down one level (-1) and Actions "Acciones" goes up one level (+1) (Also can interact with the room's stuff).

The way player's move in the room works, but I'm stuck in one part. The Idea is when the user click the floor the player goes to that floor, that's the nice part, what I want is let the user can click just once on the floor. My problem is if I click in the floor (The same floor) when the player is moving, the Energy goes down and Actions goes up in each Input.GetMouseDown(0). And I don't know how to do that at the moment.

You can watch the code to get better what I'm trying to explain and find out where I can do the changes

Player.cs

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Player : MonoBehaviour {
 
     public enum PlayerState { Happy, Normal, Dizzy, Sleepy }
 
     public PlayerState currentState = PlayerState.Happy;
     public Sprite okiHappy, okiNormal, okiDizzy, okiSleepy;
     public bool _isFacingRight;
     public float speed = 2.0f;
     public bool canMove;
     public Vector3 currentPosition;
     public Juice juice;
     public Sprite bowlEmpty, bowlFood, bowlWater;
     public SpriteRenderer bowlSpriteRender;
     public GameObject playerBalloon, playerKeys, gameOverPanel;
     public int actions = 0;
     public Text gameOverActions, winnerActions;
 
     [HideInInspector]
     public int direction = 0;
     [HideInInspector]
     public int currentActions;
 
     private SpriteRenderer spriteRender;
     private DogFood dogFood;
     private Bowl bowl;
     private Dog dog;
     private Floor floor;
     private Text txtActions;
     private float sec = 2f;
 
     void Start () {
 
         spriteRender = GetComponent<SpriteRenderer>();
 
         dogFood = GameObject.Find("Dog Food").GetComponent<DogFood>();
         bowl = GameObject.Find("Bowl").GetComponent<Bowl>();
         dog = GameObject.Find("Doki").GetComponent<Dog>();
         txtActions = GameObject.Find("Text Actions").GetComponent<Text>();
 
 
         if(spriteRender.sprite == null || currentState == PlayerState.Happy) {
             spriteRender.sprite = okiHappy;
         }
 
         if(bowlSpriteRender.sprite == null) {
             spriteRender.sprite = bowlEmpty;
         }
 
         currentPosition = transform.position;
     
     }
     
 
     void Update () {
 
         switch(juice.lvlNumber) {
 
         case 5:
             currentState = PlayerState.Happy;
             spriteRender.sprite = okiHappy;
             break;
 
         case 4:
             currentState = PlayerState.Happy;
             spriteRender.sprite = okiHappy;
             break;
         
         case 3:
             currentState = PlayerState.Normal;
             spriteRender.sprite = okiNormal;
             break;
 
         case 2:
             currentState = PlayerState.Normal;
             spriteRender.sprite = okiNormal;
             break;
 
         case 1:
             currentState = PlayerState.Dizzy;
             spriteRender.sprite = okiDizzy;
             break;
 
         
         case 0:
             currentState = PlayerState.Sleepy;
             spriteRender.sprite = okiSleepy;
             StartCoroutine(GameOver());
             break;
 
         default:
             print("Nothing to do Here!!!");
             break;
 
         }
 
         if(currentPosition != transform.position && canMove) {
 
             MoveTowards(currentPosition);
 
 
         } else if (!canMove) {
             return;
         }
 
         if(juice.lvlNumber < 0)
             juice.lvlNumber = 0;
 
 
         if(dog.currentState == Dog.DogState.Walking && Input.GetMouseButtonDown(0)) {
             
         } else if (dog.currentState != Dog.DogState.Walking) {
 
         }
 
         if(direction == 1) {
             transform.localScale = new Vector3(1f, 1f, 1f);
         } else if(direction == -1) {
             transform.localScale = new Vector3(-1f, 1f, 1f);
         }
 
         txtActions.text = (actions.ToString());
         gameOverActions.text = (actions.ToString());
         winnerActions.text = (actions.ToString());
 
         if(bowl.currentState == Bowl.BowlState.Empty) {
             bowlSpriteRender.sprite = bowlEmpty;
         }
 
 
     }
 
     public void Flip() {
         transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
     }
 
     public void MoveTowards(Vector3 target) {
 
         _isFacingRight = transform.localScale.x > 0;
 
         if(_isFacingRight) {
             Flip();
         }
 
         transform.position = Vector3.MoveTowards(
             transform.position, 
             target, 
             Time.deltaTime * speed );
 
     }
 
 
     public void ChangeBowlSprite(){
         if(bowlSpriteRender.sprite == bowlEmpty && dogFood.canFeedDog) {
             bowlSpriteRender.sprite = bowlFood;
             bowl.currentState = Bowl.BowlState.SignFood;
 
         }
     }
 
     IEnumerator GameOver() {
 
         yield return new WaitForSeconds(sec);
 
         gameOverPanel.SetActive(true);
 
     }
         
 }



Floor.cs

 using UnityEngine;
 using System.Collections;
 
 public class Floor : MonoBehaviour {
 
 
     public Juice juice;
 
     public GameObject[] floors;
 
     [HideInInspector]
     public int countClicks = 0;
 
     private Color hoverColor, normalColor;
     private Player player;
     private Dog dog;
     private bool canClickFloor, clickOnce;
 
 
     
     void Start () {
 
         hoverColor = new Color32(111, 159, 99, 255);
         normalColor = new Color32(255, 255, 255, 255);
 
         player = GameObject.Find("Oki").GetComponent<Player>();
         dog = GameObject.Find("Doki").GetComponent<Dog>();
 
     }
     
     
     void Update () {
 
         if(juice.lvlNumber < 1) {
             
             player.canMove = true;
 
         }else if(juice.lvlNumber <= 5 && player.currentState != Player.PlayerState.Sleepy) {
             
             player.canMove = true;
 
         } else if ( juice.lvlNumber <= 0 
             || player.currentState == Player.PlayerState.Sleepy || player.playerBalloon.activeSelf) {
 
             player.canMove = false;
 
         }
 
         if(dog.currentState == Dog.DogState.Walking 
             || dog.currentState == Dog.DogState.FindingBallon || player.playerBalloon.activeSelf) {
 
             player.currentPosition = player.transform.position;
 
             foreach(GameObject floor in floors) {
                 floor.GetComponent<PolygonCollider2D>().enabled = false;
             }
         }
 
         if(dog.currentState == Dog.DogState.Eating 
             || dog.currentState == Dog.DogState.HasBalloon || dog.currentState == Dog.DogState.HasKeys) {
 
             foreach(GameObject floor in floors) {
                 floor.GetComponent<PolygonCollider2D>().enabled = true;
             }
         }
 
         if(player.playerBalloon.activeSelf) {
             player.currentPosition = player.transform.position;
 
             foreach(GameObject floor in floors) {
                 floor.GetComponent<PolygonCollider2D>().enabled = false;
             }
         }
 
         if(player.currentPosition == transform.position || player.currentPosition == player.transform.position) {
             canClickFloor = true;
 
         } else if(player.currentPosition != transform.position 
             || player.currentPosition != player.transform.position) {
 
             canClickFloor = false;
         }
 
     }
 
     void MovePlayer(){
 
     }
 
 
 
     void OnMouseOver(){
         this.GetComponent<SpriteRenderer>().color = hoverColor;
 
         if(canClickFloor) {
 
             if(Input.GetMouseButtonDown(0) && player.canMove && player.transform.position.x == transform.position.x) {
                 juice.lvlNumber = juice.currentLvl;
 
             } else if(Input.GetMouseButtonDown(0) 
                 && player.canMove 
                 && player.transform.position.x != transform.position.x 
                 && player.currentState != Player.PlayerState.Sleepy) {
 
                 countClicks++;
 
                 juice.lvlNumber--;
 
                 if(transform.position.x < player.transform.position.x) {
                     player.direction = -1;
 
                 }else if (transform.position.x >= player.transform.position.x){
                     player.direction = 1;
                 }
                     
                 player.actions++;
                 player.currentPosition = transform.position;
 
                 if(dog.currentState == Dog.DogState.Eating || dog.currentState == Dog.DogState.HasBalloon) {
                     dog.dogCount++;
                 }
 
 
             } else if (Input.GetMouseButtonDown(0) 
                 && !player.canMove && player.currentState == Player.PlayerState.Sleepy) {
                 player.currentPosition = player.transform.position;
 
             }
                 
         }else {
             return;
         }
 
 
     }
 
     void OnMouseExit(){
         this.GetComponent<SpriteRenderer>().color = normalColor;
     }
 }

 

And If you need to be more clear what I'm try to explain let me show you a screenshot xD

alt text

By the way, Thank you very much!

game.png (205.9 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by aliens9889 · Oct 04, 2016 at 02:57 AM

I could fix my own problem xD. First I added a boolean on Player.cs public bool isMoving;.

Then in Update() added this condition:

 if(currentPosition != transform.position && canMove) {
 
             MoveTowards(currentPosition);
             isMoving = true;
 
         } else if(currentPosition == transform.position && canMove) {
             isMoving = false;
 
         } else if (!canMove) {
             return;
         }

And finally on Floor.cs updated the OnMouseOver() with this

 void OnMouseOver(){
         this.GetComponent<SpriteRenderer>().color = hoverColor;
 
         if(canClickFloor) {
 
             if(Input.GetMouseButtonDown(0) && player.canMove && player.transform.position.x == transform.position.x) {
                 juice.lvlNumber = juice.currentLvl;
 
             } else if(Input.GetMouseButtonDown(0) 
                 && player.canMove 
                 && player.transform.position.x != transform.position.x 
                 && player.currentState != Player.PlayerState.Sleepy) {
 
                 countClicks++;
 
                 juice.lvlNumber--;
 
                 if(transform.position.x < player.transform.position.x) {
                     player.direction = -1;
 
                 }else if (transform.position.x >= player.transform.position.x){
                     player.direction = 1;
                 }
 
                 if(player.isMoving) {
                     player.actions = player.currentActions;
                     juice.lvlNumber = juice.currentLvl;
                 } else {
                     player.actions++;
                 }
                     
                 player.currentPosition = transform.position;
 
                 if(dog.currentState == Dog.DogState.Eating || dog.currentState == Dog.DogState.HasBalloon) {
                     dog.dogCount++;
                 }
 
 
             } else if (Input.GetMouseButtonDown(0) 
                 && !player.canMove && player.currentState == Player.PlayerState.Sleepy) {
                 player.currentPosition = player.transform.position;
 
             }
                 
         }else {
             return;
         }
     }

And the Actions and Energy have the right behaviour as was planed at first.

Thanks anyway!!

Comment
Add comment · Share
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

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.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Why didnt my Collider works? 0 Answers

Make objects invisable when they are passing through 2 Answers

2d shooting problem 1 Answer

How to Implement a “Parry” mechanic to a 2D game? 1 Answer

Death Counter dont work when changing scenes 2 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