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

How Could I Get An Orientation Value That Looks Towards A Part?

How would you be able to set a part's orientation to look at another part?

Example: If the enemy hits the player, the player's humanoidrootpart will turn to look at the enemy using orientation.

I've tried using this to get the opposite value of the Y axis:

script.Parent.HumanoidRootPart.Orientation = Vector3.new(0 ,Enemy.HumanoidRootPart.Orientation.Y - (Enemy.HumanoidRootPart.Orientation.Y * 2), 0)

but sometimes the Y axis is a negative number so this approach doesn't work. Is there a reasonable way to accomplish this? Thanks.

1 answer

CFrame

There are many different CFrame constructors, but one in particular is very useful. This CFrame constructor takes two Vector3's, which are just positions, and returns a new CFrame with your desired position and orientation.

####Constructor: CFrame.new(Vector3, Vector3)

The first argument represents the fixed position of the CFrame, while the second argument represents the position that the previous position will look at. Just imagine having two points in 3D space and standing near one of them, while looking at the other. Here's an example:

local position = Vector3.new(0, 0, 0)
local lookAt = Vector3.new(0, 100, 100)

local posWithDirect = CFrame.new(position, lookAt)

The code above will create a new CFrame located at position and oriented towards lookAt. Hope this helps; let me know if you have any questions.

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