# [QUESTION] setElementRotation and getCameraMatrix.

## Recommended Posts

Problem:

When given a point P (pX, pY, pZ) how do I set an elements rotation R (rX, rY, rZ) (for example: a rocket projectile) so that the element directly faces the point P?

Context:

Basically I had an idea for a cruise missile script which involves using the camera matrix and getting the point the camera is directly looking at and setting a rocket projectile's rotation to face it. The idea is to have the rocket's rotation always point where the camera is facing so that eventually the rocket will hit or intersect that point. I have no idea how to solve this problem and I am starting to think nobody does. If anyone knows the solution to this problem could you please explain?

[EDIT]

The point which the rocket faces may change and therefore the rockets direction needs to change which gives me another question:

Does this problem involve velocity as well if the rocket needs to change direction mid-flight?

I've tried to explain as clearly as possible. If you need any further context / some sort of example in code please ask.

Thanks.

Edited by Exilepilot

You can use the findRotation function to do that.

• 1
• Moderators

I doing stuff like that with:

```local camera = getCamera()

local matrix = getElementMatrix(camera)

local rotX, rotY, rotZ = getElementRotation(camera)```

• 1
34 minutes ago, myonlake said:

You can use the findRotation function to do that.

You've made my day Thank you very much, I appreciate such a quick reply.

25 minutes ago, IIYAMA said:

I doing stuff like that with:

```
local camera = getCamera()

local matrix = getElementMatrix(camera)

local rotX, rotY, rotZ = getElementRotation(camera)
```

When I setCameraMatrix and only specify the position of the camera and not the point it's looking at. Can the point the camera is looking at change?

For example I want to change the point the rocket is facing onClientRender.

Thank you very much. I'll post results.

[EDIT]

HMM...

I just looked into findRotation and I don't think it's enough. It seems only to be used for setPedRotation which is deprecated...

I was looking for something such as:

```local px, py, pz, lx, ly, lz = getCameraMatrix()

--[[
Solution to problem...
--]]
function getRotationFacingTargetPoint(startPoint, targetPoint)
--[[ blah blah blah
change rotation to face the point ]]
return rx, ry, rz
end

-- Create projectile and guide it towards lx, ly, lz from px, py, pz.
local p = createProjectile(localPlayer, 19, px, py, pz, 1, nil, getRotationFacingTargetPoint(px,py,pz,lx,ly,lz))```

Edited by Exilepilot

Sorry, perhaps findRotation3D helps you in this case better.

Wait, so you're essentially just wanting the camera to point forward?

Edited by myonlake
• Moderators

Denny199 his reply is very useful. This will give you everything you need.

• 1

## Create an account

Register a new account