Jump to content

Problem with createProjectile


Recommended Posts

Hello, I have a problem. Im making a map, and i used this code:

  
            local occupiedVehicle = getPedOccupiedVehicle(getLocalPlayer()) 
            local x, y, z = getElementPosition(occupiedVehicle) 
            local vx, vy, vz = getElementVelocity(occupiedVehicle) 
            local rotx, roty, rotz = getElementRotation(occupiedVehicle) 
         
            createProjectile(getLocalPlayer(), 19, x, y, z, 500, nil, 0, 0, rotz, vx, vy, vz) 

But its not going as i want... i want to make it read the rotation of the car, and make the bullet go straight where the car looks. Also its not synchronised, in my friend's screen it goes to opposite direction :/ any help?

Edited by Guest
Link to comment

I don't think that getElementRotation(occupiedVehicle) will return the rotations with x,y,z order, try different configurations (y,x,z or z,x,y), also you only setting the z rotation on the Projectile, you should set all 3 components.

Also make sure that the projectal is created only on one client at the time (projectal is the only type of element that will exist on all clients even created on one)

Link to comment

Still no luck :/

Thats my current code:

local x, y, z = getElementPosition(occupiedVehicle) 
        local vx, vy, vz = getElementVelocity(occupiedVehicle) 
    local rotx, roty, rotz = getElementRotation(occupiedVehicle) 
        createProjectile(getLocalPlayer(), 19, x, y+2, z+2, 1.0, nil, rotx, roty, rotz, vx, vy, vz) 

I also tried to change 'nil' which is the target(althought i think it only works with ID 20, which is heat seeking), to rotz, but nothing changed :/

Link to comment

@CowTurbo do you even know what is a rootElement?

@DjSt3rios as I said the order of vehicle rotation components are inverted...

also I wrongly assumed that you know what you setting the vehicle velocity as projectile velocity...

this code will calculate a correct vector direction (at lest it should)

  
local occupiedVehicle = getPedOccupiedVehicle(getLocalPlayer()) 
local x, y, z = getElementPosition(occupiedVehicle) 
local matrix = getElementMatrix(occupiedVehicle) 
local offX = 0 * matrix[1][1] + 1 * matrix[2][1] + 0 * matrix[3][1] + 1 * matrix[4][1] 
local offY = 0 * matrix[1][2] + 1 * matrix[2][2] + 0 * matrix[3][2] + 1 * matrix[4][2] 
local offZ = 0 * matrix[1][3] + 1 * matrix[2][3] + 0 * matrix[3][3] + 1 * matrix[4][3] 
vx = offX– x 
vy = offY – y 
vz = offZ - z 
 local rotx, roty, rotz = getElementRotation(occupiedVehicle) -- experiment with rotx, roty, rotz  order 
  createProjectile(getLocalPlayer(), 19, x, y, z, 500, nil, 0, 0, rotz, vx, vy, vz)  
  

Link to comment
addCommandHandler("boom",function() 
local occupiedVehicle = getPedOccupiedVehicle(getLocalPlayer()) 
local x, y, z = getElementPosition(occupiedVehicle) 
local matrix = getElementMatrix(occupiedVehicle) 
local offX = 0 * matrix[1][1] + 1 * matrix[2][1] + 0 * matrix[3][1] + 1 * matrix[4][1] 
local offY = 0 * matrix[1][2] + 1 * matrix[2][2] + 0 * matrix[3][2] + 1 * matrix[4][2] 
local offZ = 0 * matrix[1][3] + 1 * matrix[2][3] + 0 * matrix[3][3] + 1 * matrix[4][3] 
vx = offX - x 
vy = offY - y 
vz = offZ - z 
local rotx, roty, rotz = getElementRotation(occupiedVehicle) 
createProjectile(getLocalPlayer(), 19, x, y, z+1, 500, nil, 0, 0, 360 - rotz, vx, vy, vz) 
end) 

tested in game, and works

Link to comment

It works, Thank you!

Edit: I got a small problem if you can help with, I want to create the bullet in front of the car and go straight. I made something, but if i turn like 90 degrees it is not working properly. I made it like this:

createProjectile(getLocalPlayer(), 19, x, y-2.3, z+0.5, 5000, nil, 0, 0, 360 - rotz, vx, vy, vz) 

But sometimes x might need to be changed. Maybe i can fix it somehow?

Link to comment
createProjectile(getLocalPlayer(), 19, offX, offY, offZ+0.5, 5000, nil, 0, 0, 360 - rotz, vx, vy, vz) 

this should do it, but if the vehicle is too long, you will need to calculate a larger ofset (this code should create the rocket 1m in front of the vehicle center of mass)

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