My game is a small shooting game in cocos2d. The enemy generates the bullets to shoot player at intervals of time. I have created a random y , so that bullets touch the opposite edge at random heights. If the bullet touches the player the enemy wins.
But, I need to set probability for the enemy accuracy. If the probability of enemy is given as 80% accuracy? How can I set in my program? For 10 shots 8 should be straight towards player.
How can I decide which ones should go straight in 10 which one should miss. In mean time player also shoots the enemy.
Thank You.
I would do it like this.
Assume your player has position y. To get 80% accuracy choose a random number from the interval
[y - height*0.2, y + height*0.2], whereheightis the height of the screen. In general to get accuracy p choose a number from[y - height*(1-p), y + height*(1-p)]. When p = 1.0 (100%) then the bullet will be aimed at exactly the position of the player.This doesn’t mean that an enemy with 80% accuracy will fire 8 out of 10 shots exactly at the player, but the more accurate the closer the bullets are.
This of course neglects the time bullet travels, but that can be included without that much work.