|
I am developing a script which enables a player to attack GameObjects tagged "enemy". The issue I am trying to resolve is the ability to produce damage towards any closest "Enemy" object, instead of one GameObject after another. At this stage, I am only able to murder a specific tagged object, instead of any closest tagged object. Here is a chunk of the script related to my issue (the center of problem is marked in arrows): function Update () { InvokeRepeating("FindClosestEnemy", 0, scanFrequency); } -> closest : String = gameObject.name; -> Target = GameObject.FindWithTag("Enemy"); } There is a function which is triggered after SendMessage("CloseEnemy", closest). I was happy with this, because print(closest); registered the name of the "closest" enemy object, however I failed to convert the variable "closest" into a string, which could be used for GameObject.Find(string);. In place of string, I inserted "enemy". If you can answer this problem, it will help me a lot! Thanks.
(comments are locked)
|
|
After running the loop in Secondly, that's a standard "Price is Right" loop. It looks at enemies one at a time, making closest always be the nearest enemy so far. It can set closest multiple times, as it sees more enemies. So, the SendMessage should be moved to after the loop, instead of being in it. Thanks, your tips have helped me find a problem. The script is running perfectly!! except punchDmg is multipled. I suspect this is due to lag in the script? I increased frequency and delay in InvokeRepeated, which makes this effect less multiplied.
Jul 06 '12 at 01:30 AM
Mikez
(comments are locked)
|

For optimization, don't calculate the distance and the normalized vector. Because you're doing twice the same calculation (which uses an expensive square root).
Instead do that:
wow, thanks man!!