Code is working, but not optimal

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 :sunglasses:.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GreenDragon : MonoBehaviour
{
    public Animator anim;
    public Rigidbody rigid;
    public Transform targetPosition;
    public float dragonSpeed;
    public bool stoppingPosition;
    public float stopDistance = 5f;
    public AnimationClip[] attacknames; 



    // Start is called before the first frame update
    void Awake()
    {
        anim = GetComponent<Animator>();
        rigid = GetComponent<Rigidbody>();
    }


    // Update is called once per frame
    void Update()
    {
        GoToPlayer();
    }



    public void GoToPlayer()
    {

        if (!stoppingPosition)
        {
            transform.LookAt(targetPosition);
            transform.position = Vector3.MoveTowards(transform.position, targetPosition.position, dragonSpeed * Time.deltaTime);
        }
        else
        {
            stoppingPosition = true;
        }

        if (Vector3.Distance(transform.position, targetPosition.position) < stopDistance)
        {
            stoppingPosition = true;
            anim.SetBool("isIdle", false);
            anim.SetBool("isWalking", false);
            RandomAttack();
        
        if (Random.Range(0f, 1f) <= 0.5f)
        {
            Debug.Log(Random.Range(0,1));
            anim.SetBool("AttackOne", true);
        }

        if (Random.Range(0f, 1f) >= 0.5f)
        {
            Debug.Log(Random.Range(0, 1));
            anim.SetBool("Tail Attack", true);
        }
    }

        else
        {
            if (Vector3.Distance(transform.position, targetPosition.position) > stopDistance)
            {
                stoppingPosition = false;
                transform.LookAt(targetPosition);
                transform.position = Vector3.MoveTowards(transform.position, targetPosition.position, dragonSpeed * Time.deltaTime);
                anim.SetBool("AttackOne", false);
                anim.SetBool("isWalking", true);
                anim.SetBool("isIdle", false);
            }
        }
    }


    public void RandomAttack()
    {
        string[] attacknames = new string[] {"AttackOne", "Tail Attack"};
        string randomName = attacknames[Random.Range(0, attacknames.Length)];
    }

/*
    public void AttackOne()
    {
        anim.SetTrigger("AttackOne");
    }

    public void TailAttack()
    {
        anim.SetTrigger("Tail Attack");
    }
*/  


/*   // Last 2 functions don't work I'm assuming I'm NOT calling them properly do more reading in Unity API.
    public void SetRandomAttackOne()
    {
        if (Random.Range(0f, 1f) <= 0.5f)
        {
            anim.SetBool("AttackOne", true);
        }
        else
        {
            anim.SetBool("AttackOne", false);
        }
    }

    public void SetRandomTailAttack()
    {
        if (Random.Range(0f, 1f) >= 0.5f)
        {
            anim.SetTrigger("Tail Attack");
        }
        else
        {
            anim.SetBool("Tail Attack", false);
        }
    }
*/

}

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.

1 Like

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 :smile: