Search Unity

Start Soldier Scripting

Discussion in 'Scripting' started by Nails-of-Scream, Apr 14, 2019.

  1. Nails-of-Scream

    Nails-of-Scream

    Joined:
    Jul 27, 2016
    Posts:
    21
    Hello again. I would want some help with a script for Direct, being the usual Soldier script. Direct is going to have what I would call "Classes" which is that every enemy and ally have the same scripts with different variable values. If you still don't understand, see it like Soldier, Wizard and Rogue scripts where each NPC have one of these class scripts and will act similarly to someone with the same script.

    I have started all over again because the first set of scripts where messy and near unreadable.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.AI;
    3.  
    4. namespace EightDirectionalSpriteSystem
    5. {
    6.     public class SoldierActor : MonoBehaviour
    7.     {
    8.         public enum STATE { Idle, Moving, Ambushing, Attacking, Hurt, Dead };
    9.         public STATE currentState = STATE.Idle;
    10.  
    11.         // Status
    12.         public int health = 100;
    13.         public int armor;
    14.  
    15.         // Rendering
    16.         public ActorBillboard actorBillboard;
    17.         public ActorAnimation idleAnim, moveAnim, readyAnim, shootAnim, hurtAnim, deathAnim;
    18.         public SpriteRenderer rend;
    19.  
    20.         // Components
    21.         ActorList listy;
    22.         public EyeScript eyes;
    23.         Transform myTransform;
    24.  
    25.         // Booleans
    26.         public bool aware;
    27.  
    28.         // AI
    29.         public NavMeshAgent agent;
    30.         public Vector3 locationToGo;
    31.         public float accuracy = 0.6f;
    32.  
    33.         void Awake()
    34.         {
    35.             agent.updateRotation = false;
    36.             myTransform = GetComponent<Transform>();
    37.             listy = GetComponent<ActorList>();
    38.             eyes = GetComponentInChildren<EyeScript>();
    39.         }
    40.  
    41.         // Update is called once per frame
    42.         void Update()
    43.         {
    44.  
    45.            switch(currentState)
    46.             {
    47.                 case STATE.Idle:
    48.                     agent.isStopped = true;
    49.                     break;
    50.  
    51.                 case STATE.Moving:
    52.                     agent.isStopped = false;
    53.                     break;
    54.  
    55.                 case STATE.Ambushing:
    56.                     agent.isStopped = false;
    57.                     break;
    58.  
    59.                 case STATE.Attacking:
    60.                     agent.isStopped = true;
    61.                     break;
    62.                 case STATE.Hurt:
    63.                     agent.isStopped = true;
    64.                     break;
    65.             }
    66.         }
    67.         private void LateUpdate()
    68.         {
    69.             transform.rotation = Quaternion.LookRotation(agent.velocity.normalized);
    70.             if (agent.velocity.sqrMagnitude > Mathf.Epsilon)
    71.             {
    72.                 transform.rotation = Quaternion.LookRotation(agent.velocity.normalized);
    73.             }
    74.         }
    75.  
    76.         public void AmbushEnemy()
    77.         {
    78.             currentState = STATE.Ambushing;
    79.         }
    80.     }
    81. }
    This is how far I've come. ActorList is what links the class scripts together.

    The sprite system I use have some slightly difficult to animate technique. It can't be updated all the time or the sprite will freeze on the first frame. (If you don't know what sprite system then just ask)

    I have plenty more scripts to show (If I must). I really need help please.
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    What's the actual issue? Your code seems fine, though you have a lot of shared data within the SoldierActor class, things like Health and State should probably be in their own components so that your other AbcActors can use them too.
     
  3. Nails-of-Scream

    Nails-of-Scream

    Joined:
    Jul 27, 2016
    Posts:
    21
    Now when I think about it. Maybe it's better to make separate scripts then make one for each character.

    Maybe the 8 Directional Sprite System can work on separate components.