Help with basic coding?
Blitz3D Forums/Blitz3D Beginners Area/Help with basic coding?
| ||
Hey, I'm new here and pretty new to coding, I know some basics bit for the most part not that much.. I need to create a game for my computer programming class, and I have a pretty strong foundation of what I want to create, but I need help.. how would I get an enemy to follow my player?? Nothing fancy and I'm probably over thinking it, but I have no clue where to start.. the enemy doesn't need to be in 3d or anything, but instead more like a small rectangle/circle that follows the player(another circle or rect.) I would appreciate help :) |
| ||
This should help. 1. Find the angle between the enemy/player (mouse): Angle# = Atan2(MouseY()-enemyY, MouseX()-enemyX) 2. Move the enemy towards the player: moveSpeed# = 1 enemyX = enemyX + Cos(Angle) * moveSpeed enemyY = enemyY + Sin(Angle) * moveSpeed |
| ||
As an alternative. ...you can do the exact same thing without using trigonometry but using vectors instead. ...eg:vectorX# = playerX - enemyX vectorY# = playerY - enemyY vectorLength# = sqr (vectorX*vectorX + vectorY*vectorY) if (vectorLength> 0) enemyX = enemyX + (vectorX/vectorLength) * enemySpeed enemyY = enemyY + (vectorY/vectorLength) * enemySpeed endif Just make sure in this case all vars are declared as floats. |
| ||
They have game programming classes for B3D? They really should teach you the basic techniques before saying 'go code a game'. |
| ||
You can find my smart missile code from the code archives at the link below. It pretty much does what you want. Smart missile code http://www.blitzbasic.com/codearcs/codearcs.php?code=2258 |
| ||
thank you all for the help, i will try these methods to get it started hopefully |
| ||
Great examples of path creators ;) |