How to make a cannon face where the player clicked

So im trying to make it so when you click anywhere and fire a lazer the cannon will look over it at but the hovercraft is welded through Quenty’s welding script. I have no idea how I would do this.
Please help.

Set the PrimaryPart CFrame to CFrame.new(PrimaryPart.Position, Mouse.hit.p) and multiply by any extra angles to make it face the right way.

does not work because of the welding script.

That’s a common problem that I’ve dealt with before, and boy do I hate it since I’m not a good scripter. What you CAN do to bypass this situation however is to have an external script that checks for welds in the lasers/projectiles/etc. and removes them, or you can make the welding script only target parts with specific names, such as Turret or Body, instead of ClassNames.

You can achieve this by manipulating the offset of the weld for the cannon rather than setting it’s CFrame. i.e. if your cannon is welded to your hovership base, you should change the C0 of this weld to rotate the cannon relative to the base, rather than rotating the whole hovership.

You can find the value to set the C0 to set by getting your CFrame in world space using the method suggested above ( CFrame.new(cannon.Position, target.Position) ), and then converting this to a cframe relative to the base ( something like base.CFrame:toObjectSpace(otherCFrame) ).

http://wiki.roblox.com/index.php?title=CFrame#toObjectSpace

1 Like

If it is unanchored, you can use BodyGyro to rotate it. Not sure how to get the rotation CFrame though.

Create the rotation CFrame from 2 vectors:

local rotCf = CFrame.new(cannonBarrel.Position,playerMouse.Hit.p)
1 Like

could you explain a little bit more in code (does not have to be the whole thing.)? Im a little bit confused. mostly the world to space and object to space bit…

The main concept to understand is the difference between the local and world space. This article explains a bit about it:
http://wiki.roblox.com/index.php?title=CFrame#Local_and_World_Space

When you set the CFrame property of a part, you’re setting it’s position and orientation relative to the fixed world axes (and you can see these by enabling ‘Display axes’ under the view tab of studio’s ribbon bar). When you multiple two cframes (i.e. part1.CFrame = part2.CFrame * CFrame.new(0,10,0)) you’re setting the position and orientation of the second part relative to the first one. In other words, we’ve translated the second part by some amount in the first part’s local space.

This is relevant to you because you have two parts welded together. You want to set your second part to a certain CFrame which you have defined in world space, when a weld’s C0 operates as an offset from the first parts local space (the same as multiplying cframes). This means we need to find the difference between the cframe of the base and turret of the tank, so that we can correctly set the offset of our weld.
The code would look a bit like this:

local absoluteTurretCFrame = CFrame.new(turret.Position, targetPosition) -- Get the turret cframe we want (relative to world space)
local relativeCFrame = tankBase.CFrame:toObjectSpace(absoluteTurretCFrame) -- Find the difference between the base CFrame and the turret CFrame we want

tankBase.TurretWeld.C0 = relativeCFrame -- Set the offset of the weld to this value

Someone else can probably explain this a bit better but hopefully this helps

2 Likes

breaks :c

if you need code lemme know.

That looks pretty promising as we can see it has the behavior you want, and is consistently doing the wrong thing (i.e. oriented exactly backwards, and pivoting around one spot in space).

The issue here looks to do with the ‘center’ of the cannon - do you have a small part at the muzzle of the cannon which is the part you are actually translating? To save on some difficulties you probably want to have the ‘root’ of the cannon model be the part which will actually pivot (i.e. the grey sphere). You also want to make sure your part is oriented properly - when you select each of the faces (you can do this by clicking each of the surface properties at the bottom of the explorer window) make sure your ‘front’ face is actually facing the front the direction you want to shoot) and your ‘top’ face is actually on top relative to the direction of the rest of the cannon.

1 Like

yes i have a part at the front of the cannon, as the center is one big union. Confusing tho… I’ll change the rootpart of the cannon model to the part.

C0 and C1 are confusing to me…

Think of C0 and C1 like attachments
C0 is the local CFrame offset from Part0, and C1 is the local offset from Part1. C0 and C1’s world space are set to the same thing, so they will be at the same spot, but the objects will be offset by the C0 and C1.

so how would I get the cannon to face the right way and stay on the holder and the “Fire” part to move with it?

Just not gunna use it.

1 Like

CFrame.new(PrimaryPart.Position, Vector3.new(Mouse.hit.p.X, PrimaryPart.Position.Y, Mouse.hit.p.Z)

1 Like