Search Unity

Question Help Implementing a CheckMouse()

Discussion in 'Scripting' started by chaseagoldsmith, Jun 19, 2021.

?

Is this question clear?

  1. Yes

    0 vote(s)
    0.0%
  2. No

    0 vote(s)
    0.0%
  1. chaseagoldsmith

    chaseagoldsmith

    Joined:
    Sep 27, 2020
    Posts:
    4
    I am trying to have a player select an enemy to attack in a chess like game but the player clicking the enemy game object. The method of issue is called CheckMouseOnAttack() and it becomes active when a Boolean selectableEnemy is true. This boolean becomes true when a raycast sent from player "sees" the enemy 1 unit away forward, left or right from a method AttackSearch().

    The boolean rings true correctly, the issue is the CheckMouseOnAttack() doesn't seem to register the Input.GetMouseButtonUp(0). I have confirmed this with the following Debug.Log check in the CheckMouseOnAttack(); Input.GetMouseButtonUp(0) { Debug.Log("Mouse Clicked");} did not return the "Mouse Clicked."

    The following methods are attached to a gamemanager game object [public void AttackSearch()] and a player game object [void Update(), public void PlayerAttack(), public void CheckMouseOnAttack()].

    [The script below is attached to the the player and controls the players turn behavior]
    void Update()
    {
    Debug.DrawRay(transform.position, transform.forward);
    staminaText.text = playerHealth.playerStaminaCurrent.ToString();

    if (!turn)
    {
    return;
    }

    if (playerHealth.playerStaminaCurrent < 1)
    {
    playerHealth.playerStaminaCurrent = 0;
    StaminaEmpty = true;
    return;
    }
    else if (!moving && !attackTurn)
    {
    FindSelectableTiles();
    CheckMouse();
    }
    else if (moving)
    {
    Move();

    if (!moving && playerAttackTurn)
    {
    Debug.Log("Entering PlayerAttack()");
    PlayerAttack();
    }

    if (!attackTurn && !playerAttackTurn && !moving)
    {
    Debug.Log("moving is false & attack turn is false and turn should end");
    TurnManager.EndTurn();
    }
    }

    Debug.Log("The Current Stamina is " + playerHealth.playerStaminaCurrent);
    Debug.Log("Current npc health is " + npcHealth.npchealthCurrent);
    }

    [The script that has issue is listed below found in the CheckMouseOnAttack()]

    public void PlayerAttack()
    {
    //player attack phase
    if (playerMove.playerAttackTurn)
    {
    Debug.Log("player attack turn started");
    AttackSearch();
    if (npcHealth.selectableEnemy)
    {
    CheckMouseOnAttack();
    }
    else if(!npcHealth.selectableEnemy)
    {
    Debug.Log("attack Skipped");
    playerMove.playerAttackTurn = false;
    attackTurn = false;
    }
    }
    }

    [This script below is attached to the Gamemanager gameobject in a class called Tactics Move which governs all movement for enemy and player and the player inherits from this script, this script finds the enemy and turns the boolean attached to the enemy to true]

    public void AttackSearch()
    {
    Debug.Log("Searching for enemy");
    RaycastHit hit;
    if (Physics.Raycast(transform.position, Vector3.forward, out hit, 1) || Physics.Raycast(transform.position, Vector3.right, out hit, 1) || Physics.Raycast(transform.position, -Vector3.right, out hit, 1))
    {

    if (hit.collider.CompareTag("NPC"))
    {
    npcHealth.selectableEnemy = true;
    Debug.Log("Enemy Selectable Kill them");
    }
    else
    {
    Debug.Log("Aint nobody");
    noOneFound = true;
    return;
    }
    }
    }

    [The script below is attached to the player and is where the problem seems to lie]

    public void CheckMouseOnAttack()
    {
    Debug.Log("Inside CheckMouseOnAttack");
    if (Input.GetMouseButtonUp(0))
    {
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    RaycastHit hit;
    if (Physics.Raycast(ray, out hit))
    {
    if (hit.collider.CompareTag("NPC"))
    {
    Debug.Log("NPC found");
    NPCHealth n = hit.collider.GetComponent<NPCHealth>();
    if (n.selectableEnemy)
    {
    tA.AttackNPC();
    Debug.Log("CheckMouseOnAttack has been executed");
    }
    }
    }
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    If you post a code snippet, ALWAYS USE CODE TAGS:

    How to use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/

    Usually button clicks are serviced on the UP, not the DOWN... but you can do either.

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494