Problem with my bullets
Blitz3D Forums/Blitz3D Beginners Area/Problem with my bullets
| ||
Hello, I was wondering if anyone could help me with my bullet functions. All the variables are declared Global. The thing is that when the player presses spacebar the CreateBullet function is called. The new bullet entity is placed in front of the player and made to face in the same direction as the player. This is done by using a temporary Pivot entity in front of the player and then using PointEntity to point the bullet at the player. The DoBullet function moves the bullet forward and then destroys it at certain distance. However, when I use this code I only see the first bullet fired. Also, the bullet always heads in the same direction, even if the player is looking another way. The player consists of three entities, the camera, the player mesh and a pivot between them (called "pivot" in the code which acts as the parent. Could anyone give me a hint? {code} Function CreateBullet() bullet = CreateCube() temp = CreatePivot () PositionEntity temp, EntityX(pivot)+5,EntityY(pivot),EntityZ(pivot) +5 PointEntity bullet, temp bullet_dist = 0 EntityRadius bullet,1 EntityType bullet,4 bullet_exist = True End Function Function DoBullet() If bullet_exist MoveEntity bullet,0,0,player_speed#+1 bullet_dist = bullet_dist+1 If EntityCollided (bullet, 3) FreeEntity bullet bullet_exist = False End If If bullet_dist = 60 bullet_dist=0 FreeEntity bullet bullet_exist=False End If End If End Function {/code} |
| ||
Look at this code here... the bullet part of it anyway. It is something similar to what I use. but not what I use... http://www.blitzbasic.com/Community/posts.php?topic=42581 -RZ |
| ||
To get the bullet to face the same way as the player, just Rotate it, using the players global EntityPitch(), EntityRoll() and EntityYaw() |
| ||
Check out my FPS example code I wrote. Look under the "Add code to allow the player to shoot:" section... |
| ||
Yeah... Wolron is the one who helped me do this! |
| ||
Wow! Great! I think the problem was that I was using local coordinates instead of global ones. Thanks WolRon (I looked at your code more than the others, but they're great too!). And thanks to Ross for recommending EntityYaw, pitch, roll (I had forgotten those. I have another question now, I am trying to "destroy" the enemy entity once its been hit. But because I am not using types for my enemy entities (I don't understand them too well yet) I don't have a way of detecting which enemy entity was actually hit (my enemy meshes are in a normal array). I believe I can get this entity with the CollisionEntity command, but I don't get how to use it. I tried this but I get a memory access violation error: For x = 1 To CountCollisions (bullet) FreeEntity CollisionEntity(bullet,x) Next |
| ||
Try posting your code again. |
| ||
Try using the EntityCollided command. :o) |
| ||
Here's my new code:Function CreateBullet() bullet = CreateCube() bullet_dist = 0 EntityRadius bullet,1 EntityType bullet,4 bullet_exist = True ;position bullet PositionEntity bullet,EntityX(pivot),EntityY(pivot),EntityZ(pivot) ;orient bullet RotateEntity bullet,EntityPitch(pivot,1),EntityYaw(pivot,1),EntityRoll(pivot,1) End Function Function DoBullet() If bullet_exist MoveEntity bullet,0,0,player_speed#+1 bullet_dist = bullet_dist+1 If CountCollisions(bullet) >0 hit_enemy = EntityCollided (bullet,3) For x = 1 To CountCollisions (bullet) FreeEntity CollisionEntity(bullet,x) Next FreeEntity bullet FreeEntity hit_enemy bullet_exist = False remaining = remaining -1 End If If bullet_dist = 60 bullet_dist=0 FreeEntity bullet bullet_exist=False End If End If End Function |
| ||
I am going to post some code I edited for a user on blitzcoder. I am posting it because I cannot find the topic on blitzcoder anymore. ALSO I am the one that used functions and types to try and modify his code from the rat's nest MESS it was... That said, the reason this code works is because of me (and by extension Wolron and Rob and a few others who taught me about functions and types): I edited it some more... still a little messy. Now it is essentially my code with whoevers mesh creation elements in it... those are general so they really do not apply to anybody. This is how you can apply entitycollided and use arrays to do things... modify it... use the ideas from it. Have fun! |
| ||
Thanks Rook, It's a little messy but got some ideas. The ShowEntity/HideEntity for the bullet is nice. I am using CreateCube for the bullet and then FreeEntity to destroy it, but there is a certain lag (if you keep pressing the fire button the game basically stops). It's nice to just be able to have there all the time and use when needed. I think I also figured out how to use EntityCollided. This is a "demo" I am making. I plan to post the full code once I finish it (to give something back to the community). It is not something very complex, but I think beginner's could find it useful to learn the basics (I myself have using Blitz only since January). |
| ||
It seems like you can only shoot one bullet at a time. Probably you want to be able to shoot them frequently, regardless of how many bullets are still in the air. You may use an array for bullets too, and Millisecs to allow new bullets. something like if gun_ready<=millisecs() gun_ready=millisecs()+100 ; 10 rounds/s CreateBullet() endif and the CreateBullet and DoBullet Functions could be prety much as they are now, just add an array index to the Bullet variable, and a global counter that will increment with each bullet, and if it reaches the max index, reset it to zero. If the array is big enough (considering the distance that will auto-free bullets), it should work nicely. Of course, you need to parse the whole array to check the distances. But make sure to set Bullet(n) to zero after Freeentity, this way you'll be able to check if a bullet mesh is still existing before you create a new one with this bullet array index. |
| ||
Actually if you modify this:Global gun_time=750 ; time in milliseconds that the gun will firewith a lower number you can spit bullets like a hose at a three alarm fire! ; ) -RZ |
| ||
I was talking about Skys Code, not yours, Rook, of course, your code may solve the problem as well. |
| ||
Thanks for the advice everyone, sorry for the long delay in answering, but I've been busy in other projects (not Blitz related). The single bullet thing was intentional. I didn't want to complicate matters too much as I'm still quite the beginner, but I will try to make it be multiple bullet. However, I think that when I do that, I will rather use types to represent the bullets, the enemies and the player, it seems so much simpler. Still, I appreciate your suggestions. Thanks. |