|
Hey guys. . What I'm attempting to do (and having little success with) is to sort the script so that when the enemy is at for example equal to a certain point i.e: -3 in the .y axis a bullet is fired. If anyone knows the best way of doing this then I would greatly appreciate it, I'm loving unity and I'm learning quite quick, however in this case I'm confused as to why if position == (insert number here) doesn't appear to work, clearly the enemy has to cross y.3 in order to reach .y 6 for example. Right now the script is written so that if the enemy is below a certain point then he will stop, and if his STOP position is equal to what I want then he will fire. I'm assuming that if position.y == doesn't work because the enemy is moving too fast, however I would like to get around this problem without slowing the enemy movement down. Currently My enemy script is as follows
Thanks guys, I'm a little unsure how to stick a reward on my post, I cant see the option anywhere.
(comments are locked)
|
|
The reason why == doesn't work on position is due to float inaccuracy. The method to get around this is to use Mathf.Approximately or test for a range of values e.g.
Just make sure that the movement speed of the object is smaller than the range you're testing for or else it's possible that the object "leaps over" the detection zone. P.S. you can only post bounties for questions after you've gained a certain number of points. However, you can give points to people just by up voting and marking answers as correct with the green tick ;) ;) I'm currently not home, but once I get back I'll give this a shot. I was expecting it to be as I said, that I'm moving at a speed where when the frame updates I'm not on 3.0 exactly, I'm actually on 3.0012 or something similar. The answer was useful though. now I just need to go onto how to set up a delay between the shooting and the enemy moving across the screen again.
Sep 02 '10 at 03:39 PM
squall_789
(comments are locked)
|
|
You're right when you say that the object must cross the plane running through 0,3,0. Unity (and computer systems in general) operate in discrete time steps. So at one point it may be at 2.9 and in the next sample, it will be at 3.1. These distinctions are difficult to detect with the human eye, but they happen nonetheless. If it's very important that the enemy start firing as close to y=3 as possible, I'd set up a trigger collider at y=3 and instead of checking the object's position in update, have the trigger send a message to the object to start firing. With colliders, of course, you still have to make sure that the collision boundaries of the object are large enough and the movement slow enough that the object doesn't skip over the collider entirely.
Sep 02 '10 at 03:13 PM
burnumd
I did think of the collider route actually. However seeing as this is just the 1st enemy and I plan to have many other enemies throughout the stage doing identical things just from differing points across the screen, it seems that colliders are not the best answer in this case.
Sep 02 '10 at 03:36 PM
squall_789
Do you mean they'd be firing at conditions other than y=3? If so then, yeah, colliders may not be the way to go unless you just had a proximity trigger around your target (as an added bonus with this approach, you could have multiple targets as well).
Sep 02 '10 at 05:48 PM
burnumd
Yes they will be appearing all over the screen at different y axis and differing x axis.
Sep 02 '10 at 11:22 PM
squall_789
So are you just looking for proximity to their target?
Sep 03 '10 at 02:05 PM
burnumd
(comments are locked)
|
