Jump to content

Ped fire


veronapl

Recommended Posts

Hi. Sorry, i'm not speak English :(

I'm write script; ped fire m4 in player (ped kill player).

This im me code:

local plr = getLocalPlayer();
local PEDEK 
function greetingHandler ( ped )
PEDEK = ped
outputChatBox ( "działa" )
setPedControlState( PEDEK, "fire", true)
addEventHandler( "onClientRender", getRootElement(), firenow)
end
addEvent( "onGreeting", true )
addEventHandler( "onGreeting", getRootElement(), greetingHandler )
 
function firenow()
local x, y, z = getElementPosition( plr )
local pedx, pedy, pedz = getElementPosition ( PEDEK )
newangle = ( 360 - math.deg ( math.atan2 ( ( x - pedx ), ( y - pedy ) ) ) ) % 360 
setPedAimTarget ( PEDEK, x, y, z )
setPedRotation(PEDEK,newangle)
setPedCameraRotation ( PEDEK, newangle)
setPedLookAt ( PEDEK, x, y, z)  
end

This is me problem/bug - video:

http://www.youtube.com/watch?v=QIyNEFr0mtg

Ped not can kill player. Ped not fire in player. Rotation ped (only legs) is ok.

Pleas help me! Sorry very very bad English.

Polish:

Ped sie obraca, ale on i tak strzela w złym kierunku, obraca lufew strone 0 stopni. Ped ma zabic gracza, a on strzela w dziwnym kierunku i nie chce wycelowac we mnie.

Link to comment
I experienced a similar issue when scripting an ai bot, try spawning the bot, then giving it a weapon and ammo afterwards (using a timer)

From the Polish part, I understand that the problem is not related to the gun itself but the ped rotation. He's trying to say the ped is not facing the player but in different directions.

@veronapl,

Try my peds resource which you can download here: https://community.multitheftauto.com/index.php?p= ... ils&id=234

My peds were simply following me, but to make them follow you, you also need to calculate the angle in which they need to go, so I think that resource will help you.

Watch the video presentation here:

You may also use setPedAimTarget to make them aim at you which I think would be the best choice for you.

Link to comment

50p, sorry Your script unfortunately not help me :(

This is video:

http://www.youtube.com/watch?v=kJc9qJWzj7Q

This is me script + Your code. Rotation is bad :/

local plr = getLocalPlayer();
local PEDEK 
function greetingHandler ( ped )
PEDEK = ped
outputChatBox ( "działa" )
 
addEventHandler( "onClientRender", getRootElement(), firenow)
end
addEvent( "onGreeting", true )
addEventHandler( "onGreeting", getRootElement(), greetingHandler )
 
function firenow()
                   local x, y, z = getElementPosition( plr );
				local X, Y = 0, 0;
				local px, py, pz = getElementPosition( PEDEK );						
					X = math.abs( x - px );
					Y = math.abs( y - py );
					faceMe = math.deg( math.atan2( Y , X ) );
					--faceMe = ( 360 - math.deg ( math.atan2 ( ( px - x ), ( py - y ) ) ) ) % 360
 
 
					if ( x >= px ) and ( y > py ) then		-- north-east
						faceMe = 90 - faceMe
					elseif ( x <= px ) and ( y > py ) then	-- north-west
						faceMe = 270 + faceMe
					elseif ( x >= px ) and ( y <= py ) then	-- south-east
						faceMe = 90 + faceMe
					elseif ( x < px ) and ( y <= py ) then 	-- south-west
						faceMe = 270 - faceMe
					end
					--guiSetText( label, tostring( faceMe ) );
 
				setPedRotation( PEDEK, faceMe );
				setPedLookAt( PEDEK, x, y, z + .5 );
                   setPedAimTarget ( PEDEK, x, y, z )
                   setPedControlState( PEDEK, "fire", true)
end

Very pleas, help.

PS

My ped is bodyguard, in my house.

Link to comment

It seems that it's MTA's bug. I just tested my peds script, I gave them weapons (/giveweapon ) and they weren't aiming in the correct direction but they were walking in my direction between the shots. Like sniper rifle, when reloading you can walk a bit before the next shot. I made them aim my head and sometimes I did get hit but most of the time they missed me, even when I was standing still.

Link to comment

check if this works:

-- absolute angle XY-plane relative to world (from origin to target)
function getAngle2D(originx, originy, targetx, targety)
   dx = targetx - originx
   dy = targety - originy
   angle = math.deg(math.atan2(dx,dy))
   -- at this point we have an angle in range 0,180 and -0,-180
   -- where values -0,-180 are values 0,180 in game
   -- and values 180,0 are 180,360 in game
   -- next 2 lines fix angle to be 0,360 correlative with MTA angles
   angle = - angle
   if(angle<0) then angle = 360.0 + angle end
   return angle
end

there is a similar function in wiki but i cant find it :(

Directly in your code:

function firenow()
   local x, y, z = getElementPosition( plr )
   local X, Y = 0, 0             
   local px, py, pz = getElementPosition( PEDEK )
   X = math.abs( x - px )
   Y = math.abs( y - py )
 
   faceMe = -math.deg( math.atan2( Y , X ) )
   if(faceMe<0) then faceMe = 360.0 + faceMe end                  
 
   --guiSetText( label, tostring( faceMe ) )    
 
   setPedRotation( PEDEK, faceMe )              
   setPedLookAt( PEDEK, x, y, z + .5 )                   
   setPedAimTarget ( PEDEK, x, y, z )                    
   setPedControlState( PEDEK, "fire", true)
end

i havent tested

Link to comment

The script from the second video seems to be almost perfect. Only 1 problem: He just rotates in the opposite direction. So I think this will pretty much solve your problem:

local plr = getLocalPlayer();
local PEDEK
function greetingHandler ( ped )
PEDEK = ped
outputChatBox ( "działa" )
 
addEventHandler( "onClientRender", getRootElement(), firenow)
end
addEvent( "onGreeting", true )
addEventHandler( "onGreeting", getRootElement(), greetingHandler )
 
function firenow()
local x, y, z = getElementPosition( plr );
local X, Y = 0, 0;
local px, py, pz = getElementPosition( PEDEK );                  
                 X = math.abs( x - px );
                 Y = math.abs( y - py );
                 faceMe = math.deg( math.atan2( Y , X ) );
--faceMe = ( 360 - math.deg ( math.atan2 ( ( px - x ), ( py - y ) ) ) ) % 360
 
 
if ( x >= px ) and ( y > py ) then      -- north-east
                    faceMe = 90 - faceMe
elseif ( x <= px ) and ( y > py ) then   -- north-west
                    faceMe = 270 + faceMe
elseif ( x >= px ) and ( y <= py ) then   -- south-east
                    faceMe = 90 + faceMe
elseif ( x < px ) and ( y <= py ) then    -- south-west
                    faceMe = 270 - faceMe
end
--guiSetText( label, tostring( faceMe ) );
 
setPedRotation( PEDEK, 360-faceMe );   -- 360-rotation makes the ped rotation exactly opposite; which appears to be just what you need
setPedLookAt( PEDEK, x, y, z + .5 );
setPedAimTarget ( PEDEK, x, y, z )
setPedControlState( PEDEK, "fire", true)
end

Link to comment
Gamesnert, not repair script :( Rotation Legs is ok, rotation torso is bad :/ Why?

4nd video :(https://www.youtube.com/watch?v=sh5xOVBF93I

It seems that it's MTA's bug. I just tested my peds script, I gave them weapons (/giveweapon ) and they weren't aiming in the correct direction but they were walking in my direction between the shots. Like sniper rifle, when reloading you can walk a bit before the next shot. I made them aim my head and sometimes I did get hit but most of the time they missed me, even when I was standing still.
Link to comment
  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...