Jump to content

[HELP] SetElementVelocity problem


Noah_Antilles

Recommended Posts

Hello,

I've been struggling with this last part of a script I'm making.

Basically an object spawns, and needs to be launched forwards. However, this doesn't happen. the object just sits still.

function rustlerCreateProjectile()
    local vehicle = getPedOccupiedVehicle(localPlayer)
    if(vehicle)then
	
        if getElementModel(vehicle) == 476 and rustler50calstate == 1 then
		outputChatBox ("50cal firing")
		
	elseif rustler20mmstate == 1 then
	    outputChatBox ("20mm cannons firing")
		
	elseif rustler37mmstate == 1 then 
		local x, y, z = getElementPosition(vehicle)
		local rx, ry, rz = getElementRotation(vehicle)
	    local projectile37mm = createObject (2686, x, y, z, rx, ry, rz, true)
		local projectileattach = attachElements (projectile37mm, vehicle, 0, 4.3, -0.15)
		
		setTimer (function()
		detachElements (projectile37mm)
		end, 50, 1)
		
		setTimer ( function()
        setElementVelocity(projectile37mm, 0, 0, 50) --this is the part that I cannot get to work		
		--destroyElement ( projectile37mm )
		end, 50, 1 )
		outputChatBox ("37mm cannon firing")
		end
	end	
end
bindKey("mouse1", "down", rustlerCreateProjectile)

What am I missing?

Link to comment

You are going to want to use a Matrix for these kind of stuffs it really comes in handy and also OOP.

Here's a simple projectile constructor i made which requires you to enable OOP:

local lp = localPlayer
local speed = 1
local lifetime = 1000
local id = 2686

function createProj( veh )
	local self = {
		pos = veh.matrix:transformPosition(0, 4.3, -0.15),
		rot = veh.rotation,
		vel = veh.velocity,
	}
	self.proj = Object( id, self.pos, self.rot )
	self.proj:setCollidableWith( veh, false )

	Timer(function()
		removeEventHandler( "onClientPreRender", root, self.move )
		self.proj:destroy()
		self = nil
	end,lifetime,1)

	function self.move( dt )
		self.proj:setVelocity( ((self.proj.matrix.forward*speed)+self.vel)*dt/17 )
	end
	addEventHandler( "onClientPreRender", root, self.move )

end

addEventHandler( "onClientKey", root, function( k,p )
	if k == "mouse1" and p then
		if not lp.vehicle then return end
		createProj(lp.vehicle)
	end
end )

 

  • Thanks 2
Link to comment

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...