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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Diablo/Titan Quest style attack system

Discussion in 'Scripting' started by darugylsweater, Feb 10, 2015.

  1. darugylsweater

    darugylsweater

    Joined:
    Oct 26, 2013
    Posts:
    17
    I am currently working on a game in the same sort of style as Diablo and Titan Quest and im stuck at the attack system. I need an attack system were you click on the enemy than you player goes to it than attacks, does anyone know how to do this or know of a tutorial for this. And before anyone says it I have already googled it and got nothing.
     
  2. Corpse0327

    Corpse0327

    Joined:
    Oct 4, 2014
    Posts:
    26
    You are asking for, how can i create a lion. Question is very big. You need to ask smaller questions.

    Like, how can i create teeth, how can i make lion chew, how can i make lion walk.

    I advise you to go and search how can you know player clicked which enemy as first question to ask. A hint "raycasting".

    Please split your question into smaller ones
     
  3. darugylsweater

    darugylsweater

    Joined:
    Oct 26, 2013
    Posts:
    17
    Yea i know it was a bit of a big question, and thanks for the response, did not think about raycasting.
     
  4. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,887
    Ideally, you would have a State Machine on your main character, but here's a simple version:
    1. Have a variable that stores the player's attack target - "public Transform attackTarget".
    2. When you click on the enemy, fill that in with the enemy's Transform component.
    3. When attackTarget is filled in, run a ChaseTarget() function.
    4. In ChaseTarget, check if we are closer than 1 unit away from the attackTarget, then run the Attack() function.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Player : MonoBehaviour {
    5.  
    6.     public Transform attackTarget;
    7.  
    8.     public bool isChasing;
    9.     public bool isAttacking;
    10.  
    11.     private NavMeshAgent nma;
    12.     private Animator animator;
    13.  
    14.     void Awake()
    15.     {
    16.         nma = GetComponent<NavMeshAgent>();
    17.         animator = GetComponent<Animator>();
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         if (attackTarget != null && !isChasing && !isAttacking)
    23.             StartCoroutine(ChaseTarget());
    24.     }
    25.  
    26.     IEnumerator ChaseTarget()
    27.     {
    28.         isChasing = true;
    29.  
    30.         while (Vector3.Distance(transform.position, attackTarget.position) > 1f && attackTarget != null)
    31.         {
    32.             nma.SetDestination(attackTarget.position); // Move to target
    33.  
    34.             yield return null;
    35.         }
    36.  
    37.         if (attackTarget != null)
    38.             StartCoroutine(Attack());
    39.  
    40.         nma.Stop();
    41.  
    42.         isChasing = false;
    43.     }
    44.  
    45.     IEnumerator Attack()
    46.     {
    47.         isAttacking = true;
    48.  
    49.         animator.Play("AttackAnim"); // Play attack animation
    50.  
    51.         yield return new WaitForSeconds(1f); // Wait for attack anim length
    52.  
    53.         // After animation, check if we are still close, if yes, remove health from enemy
    54.         //if (attackTarget != null)
    55.         //{
    56.         //    if (Vector3.Distance(transform.position, attackTarget.position) < 1f)
    57.         //        attackTarget.GetComponent<Health>().TakeDamage(50);
    58.         //}
    59.  
    60.         isAttacking = false;
    61.     }
    62.  
    63. }
     
    xerberuz and EliasMasche like this.
  5. Corpse0327

    Corpse0327

    Joined:
    Oct 4, 2014
    Posts:
    26
    Stardog's answer is quite simple and still usable. If your project is not so complicated, just go with it.