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

Code is working, but not optimal

Discussion in 'Scripting' started by Quincey, Mar 8, 2020.

  1. Quincey

    Quincey

    Joined:
    Aug 14, 2019
    Posts:
    46
    Hello,

    Once again I'm back with and I was wondering if there was a better way to call a function to get rid of unnecessary lines of code. I looked at a few videos online to get a better understanding of using arrays, but my codes seems convoluted. Any feedback or constructive criticism is always welcome thanks again for reading my post :cool:.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GreenDragon : MonoBehaviour
    6. {
    7.     public Animator anim;
    8.     public Rigidbody rigid;
    9.     public Transform targetPosition;
    10.     public float dragonSpeed;
    11.     public bool stoppingPosition;
    12.     public float stopDistance = 5f;
    13.     public AnimationClip[] attacknames;
    14.  
    15.  
    16.  
    17.     // Start is called before the first frame update
    18.     void Awake()
    19.     {
    20.         anim = GetComponent<Animator>();
    21.         rigid = GetComponent<Rigidbody>();
    22.     }
    23.  
    24.  
    25.     // Update is called once per frame
    26.     void Update()
    27.     {
    28.         GoToPlayer();
    29.     }
    30.  
    31.  
    32.  
    33.     public void GoToPlayer()
    34.     {
    35.  
    36.         if (!stoppingPosition)
    37.         {
    38.             transform.LookAt(targetPosition);
    39.             transform.position = Vector3.MoveTowards(transform.position, targetPosition.position, dragonSpeed * Time.deltaTime);
    40.         }
    41.         else
    42.         {
    43.             stoppingPosition = true;
    44.         }
    45.  
    46.         if (Vector3.Distance(transform.position, targetPosition.position) < stopDistance)
    47.         {
    48.             stoppingPosition = true;
    49.             anim.SetBool("isIdle", false);
    50.             anim.SetBool("isWalking", false);
    51.             RandomAttack();
    52.        
    53.         if (Random.Range(0f, 1f) <= 0.5f)
    54.         {
    55.             Debug.Log(Random.Range(0,1));
    56.             anim.SetBool("AttackOne", true);
    57.         }
    58.  
    59.         if (Random.Range(0f, 1f) >= 0.5f)
    60.         {
    61.             Debug.Log(Random.Range(0, 1));
    62.             anim.SetBool("Tail Attack", true);
    63.         }
    64.     }
    65.  
    66.         else
    67.         {
    68.             if (Vector3.Distance(transform.position, targetPosition.position) > stopDistance)
    69.             {
    70.                 stoppingPosition = false;
    71.                 transform.LookAt(targetPosition);
    72.                 transform.position = Vector3.MoveTowards(transform.position, targetPosition.position, dragonSpeed * Time.deltaTime);
    73.                 anim.SetBool("AttackOne", false);
    74.                 anim.SetBool("isWalking", true);
    75.                 anim.SetBool("isIdle", false);
    76.             }
    77.         }
    78.     }
    79.  
    80.  
    81.     public void RandomAttack()
    82.     {
    83.         string[] attacknames = new string[] {"AttackOne", "Tail Attack"};
    84.         string randomName = attacknames[Random.Range(0, attacknames.Length)];
    85.     }
    86.  
    87. /*
    88.     public void AttackOne()
    89.     {
    90.         anim.SetTrigger("AttackOne");
    91.     }
    92.  
    93.     public void TailAttack()
    94.     {
    95.         anim.SetTrigger("Tail Attack");
    96.     }
    97. */  
    98.  
    99.  
    100. /*   // Last 2 functions don't work I'm assuming I'm NOT calling them properly do more reading in Unity API.
    101.     public void SetRandomAttackOne()
    102.     {
    103.         if (Random.Range(0f, 1f) <= 0.5f)
    104.         {
    105.             anim.SetBool("AttackOne", true);
    106.         }
    107.         else
    108.         {
    109.             anim.SetBool("AttackOne", false);
    110.         }
    111.     }
    112.  
    113.     public void SetRandomTailAttack()
    114.     {
    115.         if (Random.Range(0f, 1f) >= 0.5f)
    116.         {
    117.             anim.SetTrigger("Tail Attack");
    118.         }
    119.         else
    120.         {
    121.             anim.SetBool("Tail Attack", false);
    122.         }
    123.     }
    124. */
    125.  
    126. }
     
  2. pantang

    pantang

    Joined:
    Sep 1, 2016
    Posts:
    219
    Cant really test without setting up an animator but I presume you are trying to get the dragon to move towards the player when he is near? I would consider moving a lot of your if's into ontriggerenter and adding a large sphere collider, and only do you main update loop if player has entered so its not all getting called everyframe.

    As for the part that does not work, you are trying to use attackone as a Bool in on part and a trigger in the other, which ever one you have used in the animator is the one to stick with, since its an attack id imagine a trigger would be best?

    also you will get a different value in debug as you are creating a new random number.
     
    Last edited: Mar 8, 2020
    Quincey likes this.
  3. Quincey

    Quincey

    Joined:
    Aug 14, 2019
    Posts:
    46
    Pantang,

    Hey man how's it going? Thanks for the feedback, I originally used an OnTriggerEnter when I started the code, but it wasn't doing other functions. I've got the animator controller set-up and it's working properly. The only issue or concern I had was all the extra lines of code I had. Especially when it came to using an array I was trying to see if there was an easier method to call a function that would randomly pick between two attack animations.
    The small little test scene I'm playing around with is having the little dragon walk up to the cube and stop a few units from the cube and perform 1-2 of the attacks. Whenever I move the cube around I wanted the dragon to stop the attack animation and proceed to follow the cube which it does, but I'm sure this code could be cleaned up better lol :D