megabyterain Posted July 25, 2016 Share Posted July 25, 2016 I'm trying to make a 2d space game involving a sort of space dogfight between the player and some ai bots. I need help with the enemy ship automatically rotating towards the player, but not being exactly pointed towards the ship 24/7. I want the enemy ship to have some "lag" in its response to the player's movement so it's more realistic. Right now I have a model which is not realistic and would probably be too hard for the player to win: update: function(){ //blah blah var tempAngle = Math.atan2(fightSprites.player.y-fightSprites.enemy[i].y, fightSprites.player.x - fightSprites.enemy[i].x); //gets angle between player and enemy fightSprites.enemy[i].body.rotation = tempAngle + (3.141/2); //too precise } Link to comment Share on other sites More sharing options...
drhayes Posted July 25, 2016 Share Posted July 25, 2016 You could make a hidden ship that flies behind the player, maybe even following in its path. Aim the enemy toward that. It'll follow eventually and encourage the player to not stay in the same place. The enemy ships could point at a displacement of the vector of the player ship: if it's a hard AI it'll lead the ship, if easier it could lag it. Link to comment Share on other sites More sharing options...
megabyterain Posted July 26, 2016 Author Share Posted July 26, 2016 3 hours ago, drhayes said: You could make a hidden ship that flies behind the player, maybe even following in its path. Aim the enemy toward that. It'll follow eventually and encourage the player to not stay in the same place. The enemy ships could point at a displacement of the vector of the player ship: if it's a hard AI it'll lead the ship, if easier it could lag it. But then I would need to lag the hidden ship which gives me the same problem I'm having right now. 3 hours ago, drhayes said: The enemy ships could point at a displacement of the vector of the player ship point at the displacement of vector? Link to comment Share on other sites More sharing options...
noobshit Posted July 26, 2016 Share Posted July 26, 2016 var currentAngle = fightSprites.enemy[i].body.rotation; correctAngle = tempAngle + (3.141/2); difference = correctAngle - currentAngle; lag = 0.3; fightSprites.enemy[i].body.rotation += difference * lag; Maybe it will help. Link to comment Share on other sites More sharing options...
megabyterain Posted July 26, 2016 Author Share Posted July 26, 2016 9 hours ago, noobshit said: var currentAngle = fightSprites.enemy[i].body.rotation; correctAngle = tempAngle + (3.141/2); difference = correctAngle - currentAngle; lag = 0.3; fightSprites.enemy[i].body.rotation += difference * lag; Maybe it will help. That works much better; not perfect, but better. However, there is another problem. When I fly the player around the enemy the enemy will glitch and spin for a few moments. I've attached a video. I'm guessing this is because body.rotation is a value from -3.14 to 3.14 and jumps between them? How can I fix this? nimbus-record-video.webm Link to comment Share on other sites More sharing options...
CtlAltDel Posted July 28, 2016 Share Posted July 28, 2016 What you need is: http://gamedevelopment.tutsplus.com/tutorials/understanding-steering-behaviors-seek--gamedev-849 Link to comment Share on other sites More sharing options...
Recommended Posts