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 .
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);
}
}
*/
}