|
Hello, I hope you can help me and understood the main problem. Here's the code: (I tried to solve it with another way(OnTriggerStay) but it doesnt worked)
(comments are locked)
|
|
Since there's countless possible reasons why the script 'fails', I suggest: 1- slightly change all your trigger function declarations to this: 2- inside all the functions type To understand what's going on. Just enter play mode and you'll start to see what happens when a single target enters, stays, and exits. Oh and remember to remove the "Collider other" parameters if you don't use them, since they require extra calculation power. By the way, I deleted my other reply since it was misleading.
Jan 14 '12 at 06:34 AM
roamcel
Your answer was really helpful, now I see that the main problem is, that if the first mob enters the collider area it triggers mod is entering the collider area but if the second mob is entering the collider area it doesn't trigger it again. Instead of that it triggers mob is staying the same happens with leaving the area, after the first mob leaves the area nothing happens because another mob is in the collider area so is leaving just triggers if the last mob is leaving. So let me guess, to solve my problem with OnTrigger was a bad idea, so is there a better way to solve the problem?
Jan 14 '12 at 11:42 AM
munimu
Use a boolean value and check it in OnTriggerEnter. Declare inside the class: public bool alreadyfiring = true; then inside OnTriggerEnter: if (alreadyfiring == true) { CancelInvoke("Fire001"); alreadyfiring = false; } if (alreadyfiring == false) { InvokeRepeating("Fire001",0,1f); } With these instructions the turret should 'cancel' its former shooting routine, and start one anew, where you select your new target, etc., whenever a new trigger is raised. You basically get to use 'alreadyfiring' as a flag to decide what to do in regards to firing and choosing a new target. Using flags for logic is very useful. (NOTE: this code is just an approximation to explain the possible solution)
Jan 15 '12 at 06:34 AM
roamcel
Wouldn't using a Raycast or using Vector3.Distance be a lot easier than playing with triggers. It is quick to setup. Maybe a quick field of view as well.
Jan 15 '12 at 08:59 AM
doomprodigy
A Vector3.Distance may be better but I'm using a Capsule collider because I've set the Vector y to infinity (my map is a bit uneven so I can't use sphere colliders they wouldn't be that accurate). I solved the problem with your help but the advise with the bool variable still didn't worked but it worked with an int variable which counts to 10 and will be set back to 0 and if its 10 the code will continue. And as second I didn't used invokerepeating because I had problems with cancelling it instead of this I used WaitForSeconds(1) and used to OnTriggerStay so it will be called 100 times but can just be executed one times a second it don't understand why it works because it should create 6 projectiles per second but it doesn't.. it works fine a friend gave me the hint to try it with the int variable never thought it would work :/ Thank you all :) I would upvote your post if it would be possible for me hehe maybe a bit later
Jan 15 '12 at 10:22 AM
munimu
(comments are locked)
|
