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

Stop navagent at attackrange value

Discussion in 'Scripting' started by Paykoman, Oct 5, 2015.

  1. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Hi guys, there is the thing, im trying to put my game to work at 100% in wt concerns with player melee attack and right now i just have one issue. i use click to move and melee attack and right know wt i want to accomplish is if my opponent is not null and im in attackrange i want my navagent to stop move forward, wts going on right now it that when i click to attack since i use same left mouse button for melee attack my player hit the enemy and move forward pushing the opponent back... i try lots of diferente bols but i cant get it to work. In other and the enemies stop properly at attack range distance dont pushing player back since they dont have the move part that player has. the script is this and if someone can help me i appreciate a lot.

    Cheers

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : BaseCharacter
    5. {
    6.     private static Player instance;
    7.  
    8.     public static Player Instance
    9.     {
    10.         get
    11.         {
    12.             if(instance == null)
    13.             {
    14.                 instance = GameObject.FindObjectOfType<Player>();
    15.             }
    16.  
    17.             return Player.instance;
    18.  
    19.         }
    20.     }
    21.  
    22.     public static Player player;
    23.  
    24.     public static Transform opponent;
    25.  
    26.     private Vector3 targetPositon;
    27.  
    28.     public bool block;
    29.  
    30.     public bool isAttacking;
    31.  
    32.  
    33.     void Awake()
    34.     {
    35.         player = this;
    36.     }
    37.  
    38.     // Use this for initialization
    39.     void Start ()
    40.     {
    41.         damage = (int)(damage + 0.5 * strength);
    42.  
    43.         isMoving = false;
    44.  
    45.         isAttacking = false;
    46.  
    47.         targetPositon = transform.position;
    48.  
    49.         curHealth = maxHealth;
    50.  
    51.         block = false;
    52.     }
    53.    
    54.     // Update is called once per frame
    55.     void Update ()
    56.     {
    57.         if (Input.GetMouseButtonDown (0)) {
    58.             SetTargetPosition();
    59.         }
    60.  
    61.         MovePlayer ();
    62.        
    63.         if (opponent != null && Vector3.Distance (opponent.position, transform.position) < attackRange && opponent.GetComponent<Enemy>().curHealth > 0)    {
    64.             Attack ();
    65.         }
    66.  
    67.     }
    68.  
    69.     void SetTargetPosition(){
    70.  
    71.         Plane plane = new Plane (Vector3.up, transform.position);
    72.         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
    73.         float point = 0;
    74.        
    75.         if(plane.Raycast(ray, out point)){
    76.             targetPositon = ray.GetPoint(point);
    77.         }
    78.     }
    79.  
    80.     private void MovePlayer(){
    81.             navAgent.SetDestination (targetPositon);
    82.             navAgent.Resume ();
    83.     }
    84.  
    85.     protected override void Attack()
    86.     {
    87.         if (Input.GetMouseButtonUp (0))
    88.         {
    89.             if(!block)
    90.             {
    91.                 isAttacking = true;
    92.  
    93.                 navAgent.Stop ();
    94.  
    95.                 transform.LookAt(opponent);
    96.  
    97.                 opponent.GetComponent<Enemy>().GetDamage(damage);
    98.  
    99.                 Debug.Log("Hit: " + damage);
    100.  
    101.                 block = true;
    102.                    
    103.                 Invoke("UnBlock", attackSpeed);
    104.             }
    105.             else
    106.             {
    107.                 isAttacking = false;
    108.             }
    109.         }
    110.     }
    111.  
    112.     void UnBlock()
    113.     {
    114.         block = false;
    115.     }
    116. }
    117.  
    PS: All the base stats as attackspeed, attack range and so one inherites from BaseCharacterClass, but thats not the issue here.
     
  2. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    In both Update() and Attack() you check for the mouse button being pressed, so both functions are being allowed to run when you are in range of an opponent. Easy fix

    Code (CSharp):
    1. void Update()
    2. {
    3.     if(Input.GetMouseButtonDown(0) && !isAttacking){
    4.         SetTargetPosition();
    5.     }
    6. }
    This still might not work as it doesn't really set isAttacking to true unless you press the mouse button. You need to check if the mouse is over the opponent or not before allowing attack or move.
     
  3. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    The mouse over is already working.. since i cant damage if its not over the enemy. i will try that and i tell u in a minute ;)
     
  4. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    Ok doing that i hv 2 issues,

    - it keeps moving and after i kill 1st one im not alowed anymore to move... -.- this is some simple stuff and i get very complicated right now..

    if i change the attack mouse button to right it works fine but dont dettect the distance to attack, and i would really like to melee attack left and somekind of spell right mouse button...
     
  5. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    If anyone know at least how can i stop pushing the enemies back when attacking them i will appreciate
     
  6. Chris-Trueman

    Chris-Trueman

    Joined:
    Oct 10, 2014
    Posts:
    1,256
    The code speaks otherwise as it is only when you are near the enemy.
    Code (CSharp):
    1. if(opponent !=null && Vector3.Distance(opponent.position, transform.position) < attackRange && opponent.GetComponent<Enemy>().curHealth>0)
    2. {
    3.     Attack ();
    4. }
    5.  
    The ray cast you are doing is against a plane and nothing else. You need to include the terrain and the enemies within that ray cast.

    Code (CSharp):
    1. Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    2. RaycastHit hit;
    3.  
    4. if (Physics.Raycast(ray, out hit, distance))
    5. {
    6.     if (hit.collider.CompareTag("Enemy"))
    7.     {
    8.         //Attack here.
    9.     }
    10.     else if (hit.collider.CompareTag("Terrain"))
    11.     {
    12.         //Move here
    13.     }
    14. }
    15.  
    This of course needs colliders on the terrain and the enemies to work. This is the general idea of it, you use whatever you like to identify the difference between the objects it doesn't need to be tags.

    Making a game is never simple.

    As for the other problem you have I have no idea why its happening in your project. With a project I am working on my enemy attacks me and I fly back quite far, the colliders are attached to the bones and the animation moves the colliders so when he attacks me the colliders hit my colliders and pushes me back.
     
    Paykoman likes this.
  7. Paykoman

    Paykoman

    Joined:
    Jun 26, 2014
    Posts:
    500
    All my enemies and terrain use collider already. So u tell me to call the attack and move function in that block of code u text me instead of the update? Or i call it in update to? Ty and sry for all the questions..

    anyway i test both ways and with that changes the player simply dont move.... i think i will keep this for now and move a bit other sbjects and try fix this later if i dont get any other solution for now
     
    Last edited: Oct 6, 2015