Hello everyone,
I have been using Unity (no coding though) for a few years but recently started learning coding in C# to create my own games. I lurked around here for quite sometime though. Before anything though, I am really sorry if this is wrong section but it looked suitable to me.
While working on the logic for a boss in my game I got stuck with coroutines; they aren’t functioning properly. Here is the code:
using UnityEngine;
using System.Collections;
public class BossOrcLogic : MonoBehaviour
{
public GameObject[] Waypoints;
public float Speed;
public float wait;
public Object Projectile;
private bool loop = true;
//private bool patrol = true;
private int currentIndex = 0;
private Vector3 currentWaypoint;
/* The code handles the movement process of the Orc where it starts with the orc going the right. After going there, the orc grabs
one of the skulls and then throw it at the player. He then moves to the left and repeats the same grabbing and throwing.
The orc throws multiple skulls at 50% and summons zombies at 75% health.*/
void Start()
{
currentWaypoint = Waypoints[currentIndex].transform.position;
}
void Update()
{
// Get the enemy's HP every frame to test the progress of the player against the boss.
int hp = gameObject.GetComponent<EnemyCollisions>().hp;
if (hp > hp / 2)
{
NormalPatrol();
}
// Test if the player has reduced the boss to 50%? Then make boss aggressive.
else if (hp <= hp / 2 && hp > (hp / 4 * 3))
{
AggressivePatrol();
}
// Test if the player has reduced the boss to 75%? Then start summoning sequences and after that keep making him aggressive.
else if (hp <= (hp / 4 * 3) && hp > 0)
{
SummonSequence();
}
// Test if the player has reduced the boss to 0%? Then kill the boss
else if (hp <= 0)
{
gameObject.GetComponent<EnemyCollisions>().DestroyEnemy(false);
}
}
void NormalPatrol()
{
if (transform.position != currentWaypoint)
{
gameObject.GetComponent<Animator>().SetFloat("x", 1);
if (currentIndex == 0)
{
//gameObject.GetComponent<Animator>().SetFloat("x", 1);
gameObject.GetComponent<Animator>().SetBool("right", false);
}
else
{
gameObject.GetComponent<Animator>().SetBool("right", true);
}
transform.position = Vector3.MoveTowards(transform.position, currentWaypoint, Speed * Time.deltaTime);
}
else
{
//StartCoroutine(TakeSkulls());
//StartCoroutine(ThrowSkulls());
StartCoroutine(WaitTime());
gameObject.GetComponent<Animator>().SetFloat("x", 0);
StartCoroutine(WaitTime());
gameObject.GetComponent<Animator>().SetFloat("x", -1);
Invoke("GetNextWaypoint", wait);
loop = true;
}
}
void GetNextWaypoint()
{
if (currentIndex >= 1)
{
if (loop)
{
currentIndex = 0;
currentWaypoint = Waypoints[currentIndex].transform.position;
loop = false;
}
}
if (currentIndex < 1)
{
if (loop)
{
currentIndex = 1;
currentWaypoint = Waypoints[currentIndex].transform.position;
loop = false;
}
}
}
//IEnumerator TakeSkulls()
//{
// gameObject.GetComponent<Animator>().SetFloat("x", 0);
// yield return new WaitForSeconds(0.3f);
//}
IEnumerator WaitTime()
{
yield return new WaitForSeconds(0.3f);
}
//IEnumerator ThrowSkulls()
//{
// gameObject.GetComponent<Animator>().SetFloat("x", -1);
// yield return new WaitForSeconds(0.3f);
//}
void AggressivePatrol()
{
}
void SummonSequence()
{
}
}
As you can see in this section:
else
{
//StartCoroutine(TakeSkulls());
//StartCoroutine(ThrowSkulls());
StartCoroutine(WaitTime());
gameObject.GetComponent<Animator>().SetFloat("x", 0);
StartCoroutine(WaitTime());
gameObject.GetComponent<Animator>().SetFloat("x", -1);
Invoke("GetNextWaypoint", wait);
loop = true;
}
The patrolling part of the above script works just fine, my problem is with the above section (side note, I am using a Blend tree for this).
The commented parts indicate the previous method I used; I created Coroutines to start the “TakeSkull” animation ending with a wait time and then starting the “ThrowSkull” animation ending another wait time and then using the Invoke method. It didn’t work and when playing in Unity it ignored it completely and behaved all weird with the animation doing weird stuff.
So I changed it to Invoke(“TakeSkulls”, 0.3f); instead and changed the IEnumerator to a normal function. Didn’t help much.
After too much weird stuff, I did what you see now above. Now the below part:
gameObject.GetComponent<Animator>().SetFloat("x", 0);
is being ignored completely and the below part:
gameObject.GetComponent<Animator>().SetFloat("x", -1);
runs twice.
The animation looks flawless but it isn’t what I want, I want it to run the x = 0 animation and then the x = -1 animation.
I am seriously at a loss what is going on and have been at this for 2.5 days now. Any help would be really appreciated and thank you very much in advance for your time and assistance.