Jump to content


Photo

How to make object shoot.


  • Please log in to reply
16 replies to this topic

#1 Lockpwnage

Lockpwnage

    GMC Member

  • New Member
  • 28 posts
  • Version:Unknown

Posted 03 January 2012 - 02:41 AM

Here is the code I have. It's not working, I can't figure out why but it doesn't.



I'm trying to have 2 allied A.I. shoot bullets at zombies when they come in range. Note: These npcs are NOT controlled by players. I already know how to make the player shoot, im looking for how to make the A.I. shoot.

Thank you to all that help.


EDIT: Ok I changed it I got the A.I. to shoot but it only shoots up I used this code instead. I want the bullet to shoot towards a zombie.

instance_nearest(x, y, enemy_parent)
instance_create(x, y, obj_bullet)

Edited by Lockpwnage, 03 January 2012 - 03:21 AM.

  • 0

#2 amiel

amiel

    GMC Member

  • GMC Member
  • 1122 posts
  • Version:Unknown

Posted 03 January 2012 - 03:10 AM

I think this part is the problem?
if(obj_npc1.y < y
you only check if the npc is above the player?is it should be that way?
To make the NPC shoot towards the zombie,
///shoot key
{
     var bb;
     bb = instance_create(x,y,obj_bullet);///use to get the id of bullet object
    with(bb)////after we get the id,we call it
    {
         var near;
         near = instance_nearest(x,y,obj_zombie);////use to get the id of zombie object
         speed = 12;
         direction = point_direction(x,y,near.x,near.y)///go BROOM!!! 
         motion_set(direction,speed);
    }
}
:whistle:

Edited by amiel, 03 January 2012 - 03:23 AM.

  • 0

#3 Lockpwnage

Lockpwnage

    GMC Member

  • New Member
  • 28 posts
  • Version:Unknown

Posted 03 January 2012 - 03:17 AM

Is wth (bb) suppose to be besides itself? Because in the code it's red. Unknown Function or Script.

Edited by Lockpwnage, 03 January 2012 - 03:21 AM.

  • 0

#4 amiel

amiel

    GMC Member

  • GMC Member
  • 1122 posts
  • Version:Unknown

Posted 03 January 2012 - 03:21 AM

Is wth (bb) suppose to be besides itself? Because in the code it's red.

sorry it's a typo,it should be with()
:whistle:
  • 0

#5 Lockpwnage

Lockpwnage

    GMC Member

  • New Member
  • 28 posts
  • Version:Unknown

Posted 03 January 2012 - 03:22 AM


Is wth (bb) suppose to be besides itself? Because in the code it's red.

sorry it's a typo,it should be with()
:whistle:



Thank you, I will try this and respond with my findings.
  • 0

#6 Lockpwnage

Lockpwnage

    GMC Member

  • New Member
  • 28 posts
  • Version:Unknown

Posted 03 January 2012 - 03:25 AM


Is wth (bb) suppose to be besides itself? Because in the code it's red.

sorry it's a typo,it should be with()
:whistle:



Alright, I tried it the npc1 now shoots to the right, I want him to shoot to the left and at the zombies.
  • 0

#7 amiel

amiel

    GMC Member

  • GMC Member
  • 1122 posts
  • Version:Unknown

Posted 03 January 2012 - 03:34 AM

you should erase or delete,all of events on the bullet object...specially the one that set it it's motion.
also that code above seems to be working,you just make sure you put them on the right place.Inside the NPC shoot key or maybe where you use to put the bullet creation on NPC?
:whistle:
  • 0

#8 Lockpwnage

Lockpwnage

    GMC Member

  • New Member
  • 28 posts
  • Version:Unknown

Posted 03 January 2012 - 03:38 AM

you should erase or delete,all of events on the bullet object...specially the one that set it it's motion.
also that code above seems to be working,you just make sure you put them on the right place.Inside the NPC shoot key or maybe where you use to put the bullet creation on NPC?
:whistle:


Should i create a new bullet for the AI? Because the bullet I have im also using for the player and if i change it, it will no longer work for the player.

Edit: I must go for the night, I will return tommorow afternoon to check this and see if I can get it working. Thank you for your help so far amiel! You've been a great deal of help with alot of my problems.

Edited by Lockpwnage, 03 January 2012 - 03:43 AM.

  • 0

#9 amiel

amiel

    GMC Member

  • GMC Member
  • 1122 posts
  • Version:Unknown

Posted 03 January 2012 - 03:42 AM

Usually bullet objects are just blank or maybe contains a code that sets it's motion.But in your case and that code above already set the motion,so creating another bullet object for all the npc's would be great.You see it is not required but just to make things run better.
:whistle:
  • 0

#10 MasterOfKings

MasterOfKings

    The True Master

  • GMC Member
  • 4888 posts
  • Version:GM8

Posted 03 January 2012 - 07:25 AM

Optimised version of amiel's code...
with (instance_create(x,y,obj_bullet)) {
    var near;
    near=instance_nearest(x,y,obj_zombie);
    speed=12;
    direction=point_direction(x,y,near.x,near.y);
}
There's no need to use motion_set(). You've already done that by setting speed and direction. You're just making GM do this...
direction=direction;
speed=speed;

In relation to bullets, they should only deal with their own movement and collisions. How they move should be given to them at creation. If it's a simple bullet that goes in a straight line, it should be a simple case of using the code above, and nothing else (well, you should add collision code).
  • 0

#11 amiel

amiel

    GMC Member

  • GMC Member
  • 1122 posts
  • Version:Unknown

Posted 03 January 2012 - 11:31 AM

you should destroy the bullets if they collide with wall or they are outside the room.
And yes motion_set is not needed,but there are cases that the bullets don't move...specially when the fps go down.
:whistle:

Edited by amiel, 03 January 2012 - 11:31 AM.

  • 0

#12 MasterOfKings

MasterOfKings

    The True Master

  • GMC Member
  • 4888 posts
  • Version:GM8

Posted 03 January 2012 - 03:14 PM

And yes motion_set is not needed,but there are cases that the bullets don't move...specially when the fps go down.

If the fps is down, motion_set() won't help.
  • 0

#13 Lockpwnage

Lockpwnage

    GMC Member

  • New Member
  • 28 posts
  • Version:Unknown

Posted 03 January 2012 - 07:59 PM

I have it set that the player's bullets go to the cursor. So im going to try creating new bullets for the NPCs to use and set them almost the same way.
  • 0

#14 Lockpwnage

Lockpwnage

    GMC Member

  • New Member
  • 28 posts
  • Version:Unknown

Posted 03 January 2012 - 10:08 PM

Alright, I got the NPC to shoot a bullet, and to the right direction, and to hit the target and do damage. All i have left to do is have the NPC fire multiple when they get close.

I created a new bullet.
I created a collision with bullet.


Heres the code for the bullet2 and the npc1.

NPC1
Event Create: instance_create(x,y,obj_bullet2)

Bullet2

Event Create : move_towards_point(-x,y,8)


I know that only makes the NPC fire one bullet on creation. I'm working on being able to fire more, when enemies get close.

Edited by Lockpwnage, 03 January 2012 - 10:08 PM.

  • 0

#15 MasterOfKings

MasterOfKings

    The True Master

  • GMC Member
  • 4888 posts
  • Version:GM8

Posted 04 January 2012 - 07:16 AM

An idea is to have a variable for the number of bullets fired in a burst, and a set time to fire between bursts. For example...
/*
**  Create event
*/
shooting=false; //whether to fire or not
curBurst=0; //number of bullets fired in burst
maxBurst=3; //number of bullets to fire
time=3; //time (in steps) between bullets
minBurstTime=20; //minimum time (in steps) between bursts
maxBurstTime=40; //maximum time (in steps) between bursts

/*
**  Step event
*/
if (shooting) {
    if (curBurst<maxBurst) {
        if (alarm[0]==-1) {
            alarm[0]=time;
        }
    }
    else {
        if (alarm[1]==-1) {
            alarm[1]=minBurstTime+random(maxBurstTime-minBurstTime);
        }
    }
}

/*
**  Alarm 0 event
*/
// <- create bullet here ->
curBurst+=1;

/*
**  Alarm 1 event
*/
curBurst=0;

You need to create the system which determines whether shooting should be true or false, and the how the bullet is created.
  • 0

#16 Lockpwnage

Lockpwnage

    GMC Member

  • New Member
  • 28 posts
  • Version:Unknown

Posted 04 January 2012 - 09:09 PM

An idea is to have a variable for the number of bullets fired in a burst, and a set time to fire between bursts. For example...

/*
**  Create event
*/
shooting=false; //whether to fire or not
curBurst=0; //number of bullets fired in burst
maxBurst=3; //number of bullets to fire
time=3; //time (in steps) between bullets
minBurstTime=20; //minimum time (in steps) between bursts
maxBurstTime=40; //maximum time (in steps) between bursts

/*
**  Step event
*/
if (shooting) {
    if (curBurst<maxBurst) {
        if (alarm[0]==-1) {
            alarm[0]=time;
        }
    }
    else {
        if (alarm[1]==-1) {
            alarm[1]=minBurstTime+random(maxBurstTime-minBurstTime);
        }
    }
}

/*
**  Alarm 0 event
*/
// <- create bullet here ->
curBurst+=1;

/*
**  Alarm 1 event
*/
curBurst=0;

You need to create the system which determines whether shooting should be true or false, and the how the bullet is created.



Ok so, If I used the code like instance_create(obj_bullet) at the alarm 0 event It will have the bullet spawn and with this code the NPC will fire to a nearby enemy?

Edited by Lockpwnage, 04 January 2012 - 09:18 PM.

  • 0

#17 MasterOfKings

MasterOfKings

    The True Master

  • GMC Member
  • 4888 posts
  • Version:GM8

Posted 08 January 2012 - 12:05 PM

The system I've shown is JUST the burst. The shooting code isn't in there.

You will need to add that where I put // <- create bullet here ->

Basically, add the code shown earlier in there.
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users