# Arrow Firing

3 replies to this topic

### #1 rochowmaster

rochowmaster

GMC Member

• New Member
• 59 posts

Posted 30 April 2011 - 05:47 AM

Hey guys,

I am making a platformer. I have my enemy shoots arrows. My player has the ability to make arrows fire back at the enemy, so in the collision with arrow event, I put a simple line of code:

```hp-=15
```

but since I am using the following:
```//Gravity
if place_free(x,y+1)
{
gravity=gravity_speed
gravity_direction=270
}
if !place_free(x,y+1)
{
gravity=0
gravity_direction=0
}

if !place_free(x,y-1)
{
move_bounce_solid(false)
}

if !place_free(x+1,y)
{
move_bounce_solid(false)
}

if !place_free(x-1,y)
{
move_bounce_solid(false)
}

//Define if the enemy_unit object exists
if instance_exists(enemy)
{
enemy=instance_nearest(x,y,enemy_unit)
}

//AI
if distance_to_point(enemy_unit.x,enemy_unit.y)<=hitrange and !collision_line(x,y,enemy_unit.x,enemy_unit,obj_ground,false,true)
{
//hit enemy
hspeed=0
show_message("Hitting")
moving=false
}
else
{
if distance_to_object(instance_nearest(x,y,obj_bow))<distance_to_object(enemy_unit) and hasbow=false
{
//pick up bow automatically
move_contact_solid(point_direction(x,y,instance_nearest(x,y,obj_bow).x,y),3)
moving=true
}
else
{
if distance_to_point(enemy_unit.x,enemy_unit.y)<=shootrange and hasbow=true and !collision_line(x,y,enemy_unit.x,enemy_unit.y,obj_ground,false,true)
{
//shoot enemy with bow (if it has one)
if floor(random(20))=0
{
[u]instance_create(x,y,obj_arrow)[/u]
}
moving=false
}
else
{
if distance_to_point(enemy_unit.x,y)>hitrange
{
move_contact_solid(point_direction(x,y,enemy_unit.x,y),3)
moving=true
if speed=0
{
if !place_free(x+move_speed*5,y) or !place_free(x-move_speed*5,y)
{
vspeed=-jump_speed
}
else
{
hspeed=0
moving=false
}
}
}
}
}
}

//Facing towards the player
if x<enemy_unit.x
{
image_xscale=1
right=true
}
else
{
if x>enemy_unit.x
{
image_xscale=-1
right=false
}
}
```
... The enemy takes damage when he fires.(yes, I'm using Lasse's AI example.)
In my arrow object, in the create event, I have move_towards_point code which makes my arrow move.
I think the solution may be in the underlined snippet of code above.
is there perhaps a way to create the arrow a little away from the object?

thanks,
Rochowmaster.
• 0

### #2 Awesomeness

Awesomeness

GMC Member

• New Member
• 367 posts

Posted 30 April 2011 - 05:59 AM

```instance_create(x,y,obj_arrow)
```
you could use a variable for x, putting
```variable_name=10*dir
/*
with dir being a variable that is determined by the direction you're facing (1 for right and -1 for left),
and the 10 making it 10 pixels away from you, or however many you want
*/
instance_create(variable_name,y,obj_arrow)
/*
which makes the arrow 10 pixels to the right if you face right, or 10 pixels to the left if you face left.
*/
```
(I'm just assuming it's a 2D platformer and it only shoots to the left and right; you could do the same with y as well if you need to)

Hope that helps!

- Awesomeness

Edited by Awesomeness, 30 April 2011 - 06:06 AM.

• 0

### #3 MasterOfKings

MasterOfKings

The True Master

• GMC Member
• 4888 posts
• Version:GM8

Posted 30 April 2011 - 09:48 AM

@Awesomeness: That wouldn't work in the slightest.

@OP: You have...
`instance_create(x,y,obj_arrow);`
Get rid of the move_towards_point() part, and replace your instance_create() code with...
```with (instance_create(x,y,obj_arrow)) {
speed=6;
direction=point_direction(x,y,enemy.x,enemy.y)-3+random(6);
}```
Where you see the 3 and 6; that's the accuracy. The arrow's direction will vary by 3.

-MoK
• 0

### #4 rochowmaster

rochowmaster

GMC Member

• New Member
• 59 posts

Posted 01 May 2011 - 08:38 AM

@Awesomeness: That wouldn't work in the slightest.

@OP: You have...

`instance_create(x,y,obj_arrow);`
Get rid of the move_towards_point() part, and replace your instance_create() code with...
```with (instance_create(x,y,obj_arrow)) {
speed=6;
direction=point_direction(x,y,enemy.x,enemy.y)-3+random(6);
}```
Where you see the 3 and 6; that's the accuracy. The arrow's direction will vary by 3.

-MoK

I commend you, master of kings. Atra Esterni ono thelduin
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users