how to make an actor to create bullet towhere the anim face

Learn how to start making games.

how to make an actor to create bullet towhere the anim face

Postby Ehman » Sat Apr 28, 2018 2:39 pm

Please I want to create a game in which the fighter creates bullet(probably from guns) which goes in the direction the fighter faces asumming
Code: Select all
key down "spacebar" =>create actor "bullet" x=+5; y=0
to create bullet to the right side of the player. Now, the player faces the down ward side of the game You know the bullet will still move in the
Code: Select all
+xvelocity
direction THANKS!
- EasyHuMaN (EHMAN)
I CAN'T WAIT TO SEE YOUR RESPONSE AND TO PLAY THE GAME (+ [ ] × ) :lol: :lol: :D :D :idea:
User avatar
Ehman
 
Posts: 186
Joined: Wed Apr 18, 2018 7:56 pm
Location: MonsteRealm
Score: 1 Give a positive score

Re: how to make an actor to create bullet towhere the anim

Postby lcl » Mon Apr 30, 2018 9:57 pm

You could create a variable for tracking the direction the player is facing.

Create a new variable called facingDir. A value of 1 will tell us that the player is facing right, and a value of -1 will tell us that the player is facing left.

Player -> Create Actor -> Script Editor:
Code: Select all
facingDir = 1; // The player starts facing right

Player -> Key Down event for moving right -> Script Editor:
Code: Select all
// Your movement code is here
facingDir = 1;

Player -> Key Down event for moving left -> Script Editor:
Code: Select all
// Your movement code is here
facingDir = -1;

Player -> Key Down (Space) -> Script Editor:
Code: Select all
int bulletSpeed = 20;
Actor *newBullet = CreateActor( ... ); // Get a pointer to the new bullet actor created

// If facingDir is 1, xvelocity will be 20, and if facingDir is -1, xvelocity will be -20
newBullet->xvelocity = bulletSpeed * facingDir; // Set the xvelocity for the bullet

The lines that begin with "//" are comments.

If there's something that you find confusing, don't hesitate to ask, I will explain it to you. :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: how to make an actor to create bullet towhere the anim

Postby Ehman » Tue May 01, 2018 6:37 am

You are a million in 1 bro.
what about facing up or down (y axis)?
User avatar
Ehman
 
Posts: 186
Joined: Wed Apr 18, 2018 7:56 pm
Location: MonsteRealm
Score: 1 Give a positive score

Re: how to make an actor to create bullet towhere the anim

Postby lcl » Tue May 01, 2018 9:29 am

Oh, sorry, I thought you were making a platformer with basic shooting to left and right. But it's not that different if you want to shoot 4 ways (left, right, up, down).

Instead of -1 and 1 for left and right, which is nice for shooting only on the horizontal axis, we will now have to define 4 different directions. Let's choose these numbers:

  • 0 - right
  • 1 - up
  • 2 - left
  • 3 - down
At first look, it might seem a bit weird to start from right and go counter-clockwise from there, but there's a reason for that. Game Editor's angle system starts with 0 for right and rotates counter-clockwise from there, so 90 degrees for up, 180 for left and 270 for down. So, as you can see, our numbers align with that setup.

So, the next step is to just add the corresponding codes to the movement events to change the facingDir variable to the values listed above.

Now, you may not be familiar with angle and directional_velocity in Game Editor. These variables are actor variables (= variables, that each actor has their own values for. x and y are actor variables as well) that can be used together to move an actor in any direction by given angle with the speed of the given directional_velocity. For more information, check GE script reference: angle and GE script reference: directional_velocity.

Now, the only thing left is to adjust the shooting event. What we will do is to create a new bullet actor, set its angle equal to facingDir * 90, and then set it's directional_velocity to the speed we want to give to the bullet.
Code: Select all
int bulletSpeed = 20; // Temporary variable for the speed to give to the bullet
Actor *newBullet = CreateActor( ... ); // Get a pointer to the new bullet actor created

newBullet->angle = facingDir * 90; // Set bullet movement angle
newBullet->directional_velocity = bulletSpeed; // Set bullet movement speed
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: how to make an actor to create bullet towhere the anim

Postby tdmxross » Thu May 03, 2018 5:26 am

I am not good at programming, but this is my way to make the player shoot in the direction he is facing. Have a look at the fireball actors too. :D
Attachments
Snap 2018-05-03 at 11.00.33.png
player shooting in Left Direction
Boi source.rar
*Download This*
(83.18 KiB) Downloaded 124 times
Last edited by tdmxross on Thu May 03, 2018 5:29 am, edited 1 time in total.
GE is better than every single game engine
User avatar
tdmxross
 
Posts: 48
Joined: Sun Dec 24, 2017 7:12 pm
Location: Maharashtra, India
Score: 4 Give a positive score

Re: how to make an actor to create bullet towhere the anim

Postby lcl » Thu May 03, 2018 11:30 am

tdmxross wrote:I am not good at programming, but this is my way to make the player shoot in the direction he is facing. Have a look at the fireball actors too. :D

Yes, your solution also works, but it's needlessly complex. You use 4 variables for shooting direction, while only 1 is enough. Same goes for the fireball actor, you don't need 4 of those. Also, instead of using Draw Actor to update the bullet's x or y every frame, you could just use xvelocity, yvelocity or directional_velocity to give the actor a velocity when you create it. :)
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: how to make an actor to create bullet towhere the anim

Postby tdmxross » Fri May 04, 2018 6:41 am

lcl wrote:
tdmxross wrote:I am not good at programming, but this is my way to make the player shoot in the direction he is facing. Have a look at the fireball actors too. :D

Yes, your solution also works, but it's needlessly complex. You use 4 variables for shooting direction, while only 1 is enough. Same goes for the fireball actor, you don't need 4 of those. Also, instead of using Draw Actor to update the bullet's x or y every frame, you could just use xvelocity, yvelocity or directional_velocity to give the actor a velocity when you create it. :)


That's cool too! but this is one of my old projects, that's why it's so messed up :lol: . thanks for your reply! :D
GE is better than every single game engine
User avatar
tdmxross
 
Posts: 48
Joined: Sun Dec 24, 2017 7:12 pm
Location: Maharashtra, India
Score: 4 Give a positive score

Re: how to make an actor to create bullet towhere the anim

Postby Ehman » Fri May 04, 2018 2:51 pm

tdmxross wrote:I am not good at programming, but this is my way to make the player shoot in the direction he is facing. Have a look at the fireball actors too. :D

THANKS FOR YOUR CONTRIBUTION
User avatar
Ehman
 
Posts: 186
Joined: Wed Apr 18, 2018 7:56 pm
Location: MonsteRealm
Score: 1 Give a positive score

Re: how to make an actor to create bullet towhere the anim

Postby Ehman » Fri May 04, 2018 2:58 pm

I thought of another variable 'facingY'

Add a new event:
create actor => Script editor:
Code: Select all
facingY = 0;

Facing up code:
Key down "up" => Script editor :
Code: Select all
facingY = - 1;
facingDir=0;

facing down code:
Code: Select all
 facingY = - 1;
facingDir=0;
User avatar
Ehman
 
Posts: 186
Joined: Wed Apr 18, 2018 7:56 pm
Location: MonsteRealm
Score: 1 Give a positive score

Re: how to make an actor to create bullet towhere the anim

Postby Ehman » Fri May 04, 2018 3:01 pm

Just change the 'facingDir' to 'facingY' when you create this event:
Key down "space" => Script editor ( ... )
User avatar
Ehman
 
Posts: 186
Joined: Wed Apr 18, 2018 7:56 pm
Location: MonsteRealm
Score: 1 Give a positive score

Re: how to make an actor to create bullet towhere the anim

Postby Ehman » Fri May 04, 2018 3:03 pm

Just change the 'facingDir' to 'facingY' when you create this event:
Key down "space" => Script editor ( ... )
Last edited by Ehman on Fri Sep 07, 2018 10:39 am, edited 1 time in total.
User avatar
Ehman
 
Posts: 186
Joined: Wed Apr 18, 2018 7:56 pm
Location: MonsteRealm
Score: 1 Give a positive score

Re: how to make an actor to create bullet towhere the anim

Postby Ehman » Sat May 05, 2018 2:45 pm

And how can I make a restart button when my player is out of vision?
User avatar
Ehman
 
Posts: 186
Joined: Wed Apr 18, 2018 7:56 pm
Location: MonsteRealm
Score: 1 Give a positive score

Re: how to make an actor to create bullet towhere the anim

Postby tdmxross » Tue May 08, 2018 2:46 pm

Ehman wrote:And how can I make a restart button when my player is out of vision?

I think this will help!
Use A and D for movement, if the player leaves the view (Out of vision) a restart button will appear. you can then click on the restart button to restart the game.
check out the scripts and figure out how it works.
Attachments
Snap 2018-05-08 at 20.11.39.png
Tutorial.rar
(746.46 KiB) Downloaded 121 times
GE is better than every single game engine
User avatar
tdmxross
 
Posts: 48
Joined: Sun Dec 24, 2017 7:12 pm
Location: Maharashtra, India
Score: 4 Give a positive score

Re: how to make an actor to create bullet towhere the anim

Postby Ehman » Wed May 09, 2018 4:26 pm

Thanks
User avatar
Ehman
 
Posts: 186
Joined: Wed Apr 18, 2018 7:56 pm
Location: MonsteRealm
Score: 1 Give a positive score

Re: how to make an actor to create bullet towhere the anim

Postby tdmxross » Wed May 09, 2018 6:05 pm

Ehman wrote:Thanks

NP :D
GE is better than every single game engine
User avatar
tdmxross
 
Posts: 48
Joined: Sun Dec 24, 2017 7:12 pm
Location: Maharashtra, India
Score: 4 Give a positive score


Return to Getting Started

Who is online

Users browsing this forum: No registered users and 1 guest