Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How Do You Rotate An Object Towards Another Object?

I'm Attempting To Make A Tower Defense Game But I Keep Getting Stuck At The Tower Rotation I Have No Clue What The First Step Of This Would Be So Any Help Would Be Appreciated .

1 answer

✓ Accepted answer

The simplest way is to use the CFrame constructor that lets you define a point to look at, like so:

local tower = -- whatever your tower is
local target = -- whatever your target is

-- Create a lookTarget Vector3 using the target's position, but with the tower's Y value. This will have the effect of the tower only rotating around the Y axis, as I assume you might want. You can just set lookTarget to target.Position if you want the tower to rotate on the X axis as well
local lookTarget = Vector3.new(target.Position.X, tower.Position.Y, target.Position.Z)

-- Set the tower's CFrame to the same position as tower, but looking at the lookTarget position
tower.CFrame = CFrame.new(tower.Position, lookTarget)

This will instantly snap the tower to look at the target. You can use lerps or tweens if you want the tower to turn over time.

1 comment

0

Thank You This Worked Great :D

This discussion is preserved as it was. New answers, comments, and votes are closed.