Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

[CFRAMING] Making an object looks at another object while being attached to a movable object?

Asked by 4 years ago
Edited 4 years ago

Here's an example to make it understandable: How to make a cannon look at a target while being attached to a car?

example picture

The barrel part is welded to the cylinder using Weld, and the Weld is used to set CFrames of the whole barrel (make it rotate freely) and that's what I'm aiming for. The only problem is the CFrames math which I do not know how.

1 answer

Log in to vote
0
Answered by 4 years ago

Roblox has an example just for that on the wiki if you look at CFrame.fromMatrix

function lookAt(target, eye)
    local forwardVector = (eye - target).Unit
    local upVector = Vector3.new(0, 1, 0)
    local rightVector = forwardVector:Cross(upVector)
    local upVector2 = rightVector:Cross(forwardVector)

    return CFrame.fromMatrix(eye, rightVector, upVector2)
end

Make sure target is the target objects position and the eye is the the turret's LookVector, also make sure this is all done on the server and not the client.

So for example I would call that function like this:

ObjectA.CFrame = lookAt(ObjectB.Position, ObjectA.CFrame.LookVector)

Make sure to substitute ObjectA and ObjectB with your actual objects.

Ad

Answer this question