I have been trying to get this patrol sequence on the enemy to work as intended for the past 48 hours but with no luck.
What I have reached so far is that the code runs well and the enemy patrols left and right as expected, however the StartCoroutine(TakeSkull()); doesn’t run whatever I do. I even changed it to Invoke(“TakeSkull”, 0.3f) and changed it into a function and it didn’t work.
All I want to achieve is that the enemy moves to a waypoint, change animation to taking the skull, once finished change animation to throwing the skull, a skull is created to do so and then moves to the other waypoint to do the same exact process.
To control the animation of the enemy I have a blend tree with x =1 y = 0 for moving in the direction. x= 0 and y = 0 for taking the skull and x = -1 and y = 0 for throwing it.
Here is the code:
[1]
Anyone can shed some light or direct me to the correct method of doing this please? I'd really appreciate it. Sorry I am still a beginner when it comes to coding.
[1]: http://pastebin.com/MgbVegAv
Stick a debug in at the start of NormalPatrol() and see how often it is actually getting called.
if (hp > hp / 2)
{
NormalPatrol();
}
It’s going to be starting that function every frame that the health is more than half and if if (transform.position != currentWaypoint) is false it is going to try and start simultaneous coroutines that may clash with each other. I’d use a boolean to make sure that function is only ever going to run once at a time:
if (hp > hp / 2)
{
if (patrolling == false)
{
patrolling = true;
NormalPatrol();
}
}
Then set patrolling = false; at the end of NormalPatrol.
I’d also call StartCoroutine(ThrowSkulls()); from within TakeSkulls() so you know that ThrowSkulls is only going to happen once the boss has actually taken the skulls.
First of all most of your “hp” conditions are pointless. Things like (hp > hp / 2) is always true as long as hp is greater than 0. A value “a” is always greater than half of that value. For example 100 is greater than 50(==100/2) so is 4 greater than 2(==4/2). All those conditions are almost like constants. You might want to compare to the enemys max health.
Next thing is you can only wait inside of Coroutines. When you start a coroutine the StartCoroutine call will return immediately without waiting. It just starts the coroutine which will run independently from the code that started the coroutine. Your “WaitTime” coroutine is pretty pointless since nothing happens inside that coroutine. If you have some code / steps you want to run in sequence with a pause in between, that code has to be inside a coroutine. A coroutine, once started, is managed by Unity. It runs on the MonoBehaviour that was used to start it. A coroutine has nothing to do with a normal method. It’s something completely different. When you invoke your coroutine, it actually creates a statemachine object which you usually pass to StartCoroutine. Unity will store that statemachine object internally and go through it’s “states” automatically.