AI shooting and using a ray

Hi,

I would like to get some thoughts on I tried a tutorial which I had used for Lost in space, but only as a knife effect. I can use a gun to shoot a character and then the edit object removes the character.

I suppose the character can fire back if shot at?

All the AI does in my case is follow, but the shooting doesn’t work. Eitherway, with this system I can create some kind of system where the character can interact with the player such as the above?

Try this tutorial if you want to learn the bge.Blender Game Engine - Enemy AI with Gun - Logic Bricks

That explains a character looking for the player, I’m more looking for a character that attacks if the player does.

Did you look at the whole tutorial?The enemy shoots at the player and follows the player.

Yes, I want to attack a passive character walking around, then the character can either run or attack using a gun.

That tutorial is set for a violent character looking for a violent player. Pretty much the aim of that game tutorial.

IT seems like you need to know how to make a enemy go forwards.Here is a example png.


The state actuator is one way to do that.Here is a state actuator tutorial.BGE 6 states

All the AI does in my case is follow, but the shooting doesn’t work

so it follows, at this point you want it to shoot?

Not hard, in the ‘follow state’ cast ray with the range it should have, add it to a delay or something. when ray hit something it shoots.

so when ray hits you do:
shoot animation.
subtract health or let player die.

etc.

Here is a pythonic enemy with a ray based vision cone using scanning.


import bge
import copy
from mathutils import Vector
def main():


    cont = bge.logic.getCurrentController()
    own = cont.owner
    if own['Go']==False:
        own.worldLinearVelocity.x*=.25
        own.worldLinearVelocity.y*=.25

    #Reset Fire clock 
    if own['Fire']>=1:
        own['Fire']-=1

    #Reset forget enemy clock
    if own['Reset']>=1:
        own['Reset']-=1
        own['Go']=True

    else:
        #forget target
        if 'Target' in own:
            del own['Target']    
            
            
    #look up and store child parts and add a few properties needed later            
    if 'Gun' not in own:
        for child in own.childrenRecursive:
            if 'Turret' in child:
                own['Turret']=child
            if 'Gun' in child:
                own['Gun']=child
            own['Sweep']=0
            own['Flip']=False    
    else:
        
        #If you don't have a target sweep the cannon back and forth
        if 'Target' not in own:   
            own['Turret'].worldOrientation=own.worldOrientation 
            own['Turret'].applyRotation((0,0,own['Sweep']*.5),1)     
            if own['Flip']==False:
                if own['Sweep']<1.5:
                    own['Sweep']+=.05
                else:
                    own['Flip']=True
            else:
                if own['Sweep']>-1.5:
                    own['Sweep']-=.05
                else:
                    own['Flip']=False           
        else:
            local = own['Target'].worldPosition-own.worldPosition
            local = own.worldOrientation.inverted()*local
            
            #This defines a cone in front of the actor
            if local.y>0 and (local.x-local.y)<.25:
                #Aim at enemy if he is in front of you
                V2 = own.getVectTo(own['Target'])
                own['Turret'].alignAxisToVect(V2[1],1,.1)
                own['Turret'].alignAxisToVect(own.worldOrientation.col[2],2,1)
            else:
                #Sweep for enemy
                own['Turret'].worldOrientation=own.worldOrientation 
                own['Turret'].applyRotation((0,0,own['Sweep']*.5),1)     
                if own['Flip']==False:
                    if own['Sweep']<1.5:
                        own['Sweep']+=.05
                    else:
                        own['Flip']=True
                else:
                    if own['Sweep']>-1.5:
                        own['Sweep']-=.05
                    else:
                        own['Flip']=False         
                
                                
        rayE = own['Gun'].worldPosition+own['Gun'].worldOrientation.col[1]*15
        ray = own.rayCast(rayE,own['Gun'].worldPosition,0,'Team',0,0,0)
        if ray[0]:
            if ray[0]['Team']!=own['Team']:
                own['Reset']=60
                
                own['Target']=ray[0]  
                if own['Fire']==0:
                    bge.render.drawLine(ray[1],own['Gun'].worldPosition,(1,0,0))
                    ray[0].applyImpulse(ray[1],own['Gun'].worldOrientation.col[1]*1)
                    own['Fire']=30             
            else:
                #If you see a friendly engaged in combat and you don't have a target grab his
                if 'Target' in ray[0] and 'Target' not in own:
                    own['Target']=ray[0]['Target']
                    own['Reset']=120
    if 'RoutePlan' not in own:
        
        #Look up NavTarget 
        for child in own.children:
            if 'NavTarget' in child:
                own['NavTarget']=child
                child.removeParent()
        
        # Gather List of points to visit        
        SubList = []
        for objects in own.scene.objects:
            if 'Tag' in objects:
                if objects['Tag']==own['TagName']:
                    SubList.append([objects,objects['Route']])
        
        #sort points by value of Route            
        SubList =  sorted(SubList, key=lambda tup: tup[1])    
        #StorePlan
        own['RoutePlan']=SubList
    else:
        if 'Target' not in own:
            
            #if you dont' have a current trip plan one
            if 'Current' not in own:
                C=[]
                for point in own['RoutePlan']:
                    C.append(point)
                own['Current']= C
            else:
                own['N']=len(own['Current'])
                if len(own['Current'])>0:
                    #Move target to trip point
                    Target = own['Current'][0][0]
                    own['NavTarget'].worldPosition=Target.worldPosition+Vector([0,0,1])
                    own.alignAxisToVect((0,0,1),2,1)
                    own.alignAxisToVect(own.getVectTo(own['NavTarget'])[1],1,.1)
                    
                    own['Range']=own.getDistanceTo(own['NavTarget'])
                    # if you reach a point pop it off the trip plan
                    if own.getDistanceTo(own['NavTarget'])<.25:
                        own['Current'].pop(0)
                else:
                    #if you are out of points to visit remove plan
                    del own['Current'] 
        else: 

            #you have a target and it's more then 5 units away drive to it and aim at it
            if own.getDistanceTo(own['Target'].worldPosition)>5:
                own['NavTarget'].worldPosition = own['Target'].worldPosition
                own.alignAxisToVect((0,0,1),2,1)
                own.alignAxisToVect(own.getVectTo(own['NavTarget'])[1],1,.1)
                own['Go']=True
            #you have an enemy and it's closer than 5 units so stop moving and aim
            else:
                own.alignAxisToVect((0,0,1),2,1)
                own.alignAxisToVect(own.getVectTo(own['NavTarget'])[1],1,.1)
                own['Go']=False                            
                        
                    
                    


main()



just keep looking at scripts until it makes sense.

logic bricks are amazing - for triggering python efficiently or for doing VERY simple things.

Attachments

PythonicEnemy.blend (499 KB)

Modelinblender are you ready to learn the python programming language or do you still want to learn logic bricks?If you want to use the python programming language to make videogames.You will have to have learn the python programming language.And it will take a lot of practice.Something i have not been doing lately.

I would just use states. Like Cotax said.

You say if you shoot at a person he/she will either run or shoot back. You could use a random actuator for this, to decide to fight or flight.

So what is the trigger? I set up a policeman to “chase” when the left mouse button was pushed, FP shoots his gun. Or, do you want the person to fight or run when he is hit with a bullet, or projectile?
Whichever, This is where you could use the random actuator to decide. Play around with the random actuator to get familiar with it.

On my set up. The policeman (in your case a person) walks his beat, (state 1)
when the left mouse button is positive he changes to state 2. (Any discharge of a weapon alerts the police)

On state 2 - the policeman uses the navmesh steering to chase the player.
On state 2 - I have an empty parented on the policeman with a ray that looks for “player” distance of 100. When it sees the player it changes to state 3.

On state 3 - the policeman (person) stops, plays a shooting animation. Fires a ray to deduct health from the player. a delay changes back to state 2, where the whole process starts again if the player isn’t killed.

You can add whatever you want to your state machine.

Right, the AI just follows the player as that tutorial I watched will explain.

But no shooting actually happens, when I press the left mouse button, no idea what is up with that.

Unfortunately I’m not going to be learning any languages any time soon. Everything has to be done using the bricks. That was the reason I got involved with the Blender game engine. Remember Lost science, I had contacted you to do the programming for Phase 2 mini game.

Since I can’t seem to get the weapon system working, I’m not sure what to do about this, either I leave out the weapons, and just get a AI character to attack by kicking or something.

I stopped making your second mini game because i was having trouble implementing the videogame on the same scene.I got tired of that.I was afraid to put pieces of the game on many scenes because they may become blank.I guess i really did not know what i was doing.I think it had something to do with the animations when i duplicated the player.Did you understand anything we posted?Did you understand the youtubes videos?You need tell us what do not understand about what we posted or about the videos.How old are you really?Give me approximate age.

I don’t get any of this.

What I will have to do is for the moment just use one arm/weapon and then use that against some AI. As for the character, the character will walk as I stated, it does that if I remove the ray weapon system. So once the FP moves to the AI, then the FP attacks, the AI reacts to this event using a weapon on the FP, so the FP needs a health number or bar.

The AI will have a number of times it takes damage and is then removed, but I’ll need to have a new body spawn on the ground. I suppose that is a separate layer event too.

I’m twenty seven.

You will have to use states.learn how to use the state actuator now.Look at the youtube tutorial that i gave you name of on post 6.

The problem with the character, is the skeleton which has the walking movements, that is the child of the parent, so what is the solution with that? I was stated by one user on here in an topic about how I had to set it that way so that the FP could collide with the a character so it would fall down.

The AI just moves to its target as it should.

You mean your animation are not playing?Show me your blend.Because i do not know what you are talking about. And i believe no one else does i think.You know how to rig and animate a character?

enemy = red, player = green, player drops blood as well.
no states needed at all for this. you can let player shoot first then enemy can react to it, but that’s up to you to figure out.

enemy follows and shoot player.blend (492 KB)

if you can’t do that, then go watch tutorials on YouTube till you are able to.
(rude? yes. but you asked for it, i provided you the basics to build it).

it’s not hard, think and try, yes try to figure out a way like its shown in the blend.
you only need to add a few more bricks to get what you want. (shoot and enemy react)

I haven’t typed for anything or asked for anything.

I simply don’t understand how to do what ever, there are no brick only clips on the site, if there were I doubt I would ever of posted here. I’ve decided to have one weapon for now. I’ll take Monster’s advice on that.

The AI does walk, and is animated, if I can hit or kick the model it flys into the 3D space. Needs to have a reaction to it, so a collision which then makes the AI shoot the FP. I suppose the shooting back is the same as making one for a FP.

I guess. So I need to add a health counter to the FP, once AIbullet collides with FP -1 or something like that.

But yes, as for the weapon the FP, I tried to light a fire, a cube spawns which is the fire, but no actual textured flames or any flames are rising from it. What could be causing that?

Looking at a added plane from a certain angle you will not see it.What do you have your texture on a cube or plane?

They are using Planes, I had used a tutorial on video site a few weeks back to do this, it does work in the scene, just it doesn’t spawn once an object touches the ground texture. Only a cube. I would want that to load where ever the object lands to ignite a fire.

Do I have to duplicate that and place it in another layer? I doubt I’d need to, I don’t think that would make any sense.