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
By the way, Thank you very much!