Im trying to script up a turret right now and im confused on how mouse.target and filter work…
Also could someone explain CFrame:LookAt? There is no page on it on the developer hub.
Im trying to script up a turret right now and im confused on how mouse.target and filter work…
Also could someone explain CFrame:LookAt? There is no page on it on the developer hub.
CFrame.lookAt creates a new CFrame with a location and a direction.
For example, lets say you have a destination position that you want your part to face towards in the world as a Vector3 value
local destination = Vector3.new(10,10,10)
Then you can use this destination position in the lookAt function to say “hey, face this direction”.
The first parameter is then the object’s position that you want looking towards. But only the POSITION of the object is needed. For example, lets say you have a gun turret. You would grab the barrel and set it up like this:
local gunTurret = workspace.gunTurret
local gunOrientation = CFrame.lookAt(gunTurret.Position, direction)
where direction is the destination we got from above. This creates a direction CFrame.
Now to move the turret towards the destination, you would set it’s CFrame equal to the CFrame.lookAt()
Like this:
gunTurret.CFrame = gunOrientation
All together it would look like this:
local destination = Vector3.new(10,10,10)
local gunTurret = workspace.gunTurret
local gunOrientation = CFrame.lookAt(gunTurret.Position, direction)
gunTurret.CFrame = gunOrientation
You can also use the mouse.hit property that returns a position in worldspace that the player mouse hit instead of the hardcoded destination position.
Hope that helps
So I could use a while loop so that lets say a barrel keeps looking at my mouse?
Also another question: Do you know how I could make it so that it lets say i have a battleship cannon turret: You know how the base rotates and the barrels go up and down? How do I make it so that lets say the base only rotates left and right with the mouse and visa versa wit the barrels.
To lock an axis, you can set the position on a specific axis to 0 im pretty sure (not 100% sure). And yeah you can have it in a while loop too