Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question How can I make my mouse's position tell which direction to attack in a Top Down game?

Discussion in '2D' started by The_ZayNepz_Guy, Jul 22, 2022.

  1. The_ZayNepz_Guy

    The_ZayNepz_Guy

    Joined:
    Mar 2, 2020
    Posts:
    5
    Code (CSharp):
    1. public class movement : MonoBehaviour
    2. {
    3.     public float moveSpeed;
    4.     public Rigidbody2D rb;
    5.     private Vector2 moveDirection;
    6.  
    7.     public Animator anim;
    8.  
    9.     public Transform keyFollowPoint;
    10.     public KeyScript followingKey;
    11.  
    12.     public float attackrate = 2f;
    13.     float nextAttackTime = 0f;
    14.     bool CanMove = true;
    15.  
    16.  
    17.     public float dashSpeed;
    18.     public float dashLenght;
    19.     public float dashCooldown;
    20.     private float activeMoveSpeed;
    21.     public float dragAmount = 90f;
    22.     public bool isSticky;
    23.  
    24.  
    25.     void Update()
    26.     {
    27.         ProcessInputs();
    28.         Animate();
    29.  
    30.         float AttackX = Input.mousePosition.x;
    31.         float AttackY = Input.mousePosition.y;
    32.  
    33.         Camera.main.ScreenToWorldPoint(Input.mousePosition);
    34.         Vector3 mouseDirection;
    35.  
    36.  
    37.         if (Time.time >= nextAttackTime)
    38.         {
    39.             if (Input.GetButtonDown("attack"))
    40.             {
    41.                 Attack();
    42.                 nextAttackTime = Time.time + 1f / attackrate;
    43.  
    44.             }
    45.         }
    46.     }
    47. void Animate()
    48.     {
    49.         anim.SetFloat("AnimMoveX", moveDirection.x);
    50.         anim.SetFloat("AnimMoveY", moveDirection.y);
    51.         anim.SetFloat("AnimMoveMagnitude", moveDirection.magnitude);
    52.         //anim.SetFloat("MouseX", AttackX.x);
    53.         //anim.SetFloat("MouseY", AttackY.y);
    54.     }
    55.     void Attack()
    56.     {
    57.        
    58.     }
    I've got some progress on this but haven't gotten a full solution so far.

    I have the animations in a blend tree, set with floats X and Y.

    I know this is not the way to do it but the closest and best I could. Any help would be appreciated.
     
    Last edited: Jul 22, 2022
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    We need a bit more information. Also, screenshots of code are not the correct way to share code. Copy and paste it into this text but use the Code tags option from the ribbon. Is it that you only want the player to shoot right when facing right and the mouse is to the right? Or something else? Can you give more explanation of what outcome you are looking for.
     
  3. The_ZayNepz_Guy

    The_ZayNepz_Guy

    Joined:
    Mar 2, 2020
    Posts:
    5
    Yea, sorry for that. What I'm trying to accomplish is that the attack animation would play to the direction on the mouse in relation to the player. So that your move direction would not tell which way to attack, but it would be your mouse.
    The attack is a melee attack and all 8 directions are animated in the blend tree.
     
  4. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
    Ive never done a top-down attack like you are trying, but I would imagine you would get the mouse position X & Y, and then find out the distance from the player's position. Then you would find out what "section" the mouse is in relative to the player to fire at.

    upload_2022-7-22_12-37-33.png
     
  5. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,343
  6. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    what cornysam said, you should use the mouse pos and char pos to find an angle and based on that angle u decide the direction