Search Unity

Combat

Discussion in 'Scripting' started by Bonkahe, Aug 29, 2013.

  1. Bonkahe

    Bonkahe

    Joined:
    Jul 11, 2013
    Posts:
    14
    So basically I have a VERY simple combat system in place where when you click it draws a ray in front of you, if you hit a game object you damage game object, you tab through to target the right enemy then click to attack and damage, the problem is that when I tab through to a target, no matter what enemy I attack I hit the one targeted no matter where it is.

    My Scripts.

    Targetting:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Collections.Generic;
    5.  
    6. public class Targetting : MonoBehaviour {
    7.     public List<Transform> targets;
    8.     public Transform selectedTarget;
    9.    
    10.     private Transform myTransform;
    11.     // Use this for initialization
    12.     void Start () {
    13.         targets = new List<Transform>();
    14.         selectedTarget = null;
    15.         myTransform = transform;
    16.        
    17.         AddAllEnemys();
    18.     }
    19.    
    20.     public void AddAllEnemys()
    21.     {
    22.         GameObject[] go = GameObject.FindGameObjectsWithTag("Enemy");
    23.        
    24.         foreach(GameObject enemy in go)
    25.             AddTarget(enemy.transform);
    26.     }
    27.    
    28.     public void AddTarget(Transform enemy)
    29.     {
    30.         targets.Add (enemy);
    31.     }
    32.    
    33.     private void SortTargetsByDistance()
    34.     {
    35.         targets.Sort(delegate(Transform t1, Transform t2) {
    36.                 return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
    37.                 });
    38.     }
    39.        
    40.        
    41.     private void TargetEnemy()
    42.     {
    43.         if(selectedTarget == null)
    44.         {
    45.             SortTargetsByDistance();
    46.             selectedTarget = targets[0];
    47.         }
    48.         else
    49.         {
    50.             int index = targets.IndexOf(selectedTarget);
    51.            
    52.             if(index < targets.Count - 1)
    53.             {
    54.                 index++;
    55.             }
    56.             else
    57.             {
    58.                 index = 0;
    59.             }
    60.             DeselectTarget();
    61.             selectedTarget = targets[index];
    62.         }
    63.         SelectTarget();
    64.        
    65.     }
    66.    
    67.     public void SelectTarget()
    68.     {
    69.         selectedTarget.renderer.material.color = Color.red;
    70.         PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
    71.        
    72.         pa.target = selectedTarget.gameObject;
    73.     }
    74.    
    75.     private void DeselectTarget()
    76.     {
    77.         selectedTarget.renderer.material.color = Color.blue;
    78.         selectedTarget = null;
    79.     }
    80.    
    81.     // Update is called once per frame
    82.     void Update () {   
    83.             if(Input.GetKeyUp(KeyCode.Tab))    
    84.         {
    85. //          var ray1 = new Ray( transform.position, transform.forward );
    86.            
    87. //          var int_HitRadiusDistance = 4;
    88.        
    89. //          if (Physics.Raycast (ray1, int_HitRadiusDistance ))
    90. //          {
    91.             TargetEnemy();
    92.            
    93.         }
    94.     }
    95. }
    96.  
    PlayerAttack:
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class PlayerAttack : MonoBehaviour {
    6.     public GameObject target;
    7.     public float attackTimer;
    8.     public float coolDown;
    9.    
    10.     private Transform myTransform;
    11.    
    12.     // Use this for initialization
    13.     void Start () {
    14.         attackTimer = 0;
    15.         coolDown = 1f;
    16.    
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update ()
    21.     {
    22.         // makes sure it does devide by zero and destroy the universe.
    23.         if(attackTimer > 0)
    24.             attackTimer -= Time.deltaTime;
    25.        
    26.         if(attackTimer < 0)
    27.             attackTimer = 0;
    28.                
    29.         //controlls the actual attacking.
    30.         if(Input.GetKeyUp(KeyCode.Mouse0))
    31.         {  
    32.             if(attackTimer == 0)
    33.             {
    34.                 var ray1 = new Ray( transform.position, transform.forward );
    35.            
    36.                 var int_HitRadiusDistance = 4;
    37.            
    38.                 if (Physics.Raycast (ray1, int_HitRadiusDistance, SelectTarget ))
    39.                 {
    40.                 Attack();
    41.                 attackTimer = coolDown;
    42.                 }
    43.             }
    44.         }
    45.     }
    46.     //finally does the damage.
    47.     private void Attack(){
    48.         EnemyHealth eh = (EnemyHealth)target.GetComponent("EnemyHealth");
    49.         eh.AddjustCurrentHealth(-10);
    50.     }
    51. }
    52.  
    What ya'll think? I know I'm missing something here, also if ya'll want enemy AI and player/enemy health scripts lemmie know.