How to make this work?

Hi guys. I’d like to know how I can make a model face the direction that the NPC is facing. Let me give you some more context. You can skip to the bottom if both of these examples are TL;DR. Say for instance you have a robot. You program the robot to fire a very complex missile out every five seconds. Because the missile is complex and isn’t just one part, you group it together and have to use :PivotTo() to teleport it to the desired place. However, when the robot fires the missile, the missile is facing upward and not where the robot is facing.

A cannon would also work well as an example. In real life, a cannonball would fire where the cannon barrel is facing. Let’s put that cannon into a computer. Since you have to script the cannon, it might not always work correctly. When you test the game, your cannonball fires upward instead of where the cannon barrel is facing. How can I fix that? Do you get what I mean? How can I fix the orientation?

while true do
	FindNeededItems()
	task.wait(5)
	if not Inst or not RootPart or not Humanoid then return end
	local Attack = Inst:Clone()
	Attack.Parent = game.Workspace
	Attack:PivotTo(CFrame.new(RootPart.Position, RootPart.CFrame.RightVector))
	Attack.System["KR Enabled"].Value = true
	Attack.System.Enabled = true
end

Use something like Cframe.lookat() which would require the Position of the Object (assuming you want it to stay where it’s at, you would return it’s current position), and then the Objects Position you want to look at. If that makes sense…

In your case I think it would be something like (if not, adjust it with my explaining above haha…):

while true do
	FindNeededItems()
	task.wait(5)
	if not Inst or not RootPart or not Humanoid then return end
	local Attack = Inst:Clone()
	Attack.Parent = game.Workspace
	Attack:PivotTo(CFrame.new(RootPart.Position, RootPart.CFrame.RightVector) * CFrame.lookAt(Attack:GetPivot().Position, RootPart.CFrame.Position))
	Attack.System["KR Enabled"].Value = true
	Attack.System.Enabled = true
end

Edit: fixed an error in my script lol

1 Like

This didn’t work. It just spawns the part in the middle of nowhere. Take a gun for example. When you aim a gun and pull the trigger, the bullet won’t be positioned in the ocean, nor will it be faced upward. The bullet will face where the gun is facing. I hope that helps you.

You would still want to Use Cframe.LookAt()… To be honest I didn’t have enough information so I was making a guess, but you may know your own system more then me and able to change CFrame.lookAt() to work in your case. Here is an example:

I have 2 parts here, a Blue part, with a black “Front” face color, and a Green part. When the Green part changes position, the Blue part stays in the same position but the orientation changes. Here is the video:

Script I used:

local ChangingPart = script.Parent --Blue part with black front face
local LookAtPart = game.Workspace.LookAtPart -- Green Part

repeat --Looping it so it checks if the Green part has changed in this example. 
	task.wait(0.05)
	ChangingPart:PivotTo(CFrame.lookAt(ChangingPart:GetPivot().Position, LookAtPart:GetPivot().Position)) 
	--The first aurgment is that Position it will be in, the second is where it will be facing!
until false

Now this is a basic example that you can build on. For example a bullet from a gun would move forward in that direction, however in this example I wanted to keep it simple!

1 Like

Yes, you can just PivotTo to make the CFrame of the model the same as the NPC. This will apply the position and rotation.

If your missile faces the wrong direction, you can always define an angle offset using CFrame.Angles and use the * operator to apply it onto the pivot.

2 Likes

If you do end up offsetting with CFrame.Angles() then make sure the x, y and z values are wrapped around with math.rad(), except if the value is 0.

partA.CFrame = CFrame.new(CFrame.lookAt(partA.CFrame.Position,partB.CFrame.Position)) * CFrame.Angles(math.rad(90),0,0)
1 Like

This was the closest to what I needed, but I figured out a rather easy way to do this. There are two ways to make this work. First way:

local RootPart = script.Parent:FindFirstChild("HumanoidRootPart")
local Humanoid = script.Parent:FindFirstChildOfClass("Humanoid")
local ServerStorage = game:GetService("ServerStorage")
local Inst = nil

local function FindNeededItems()
	for i, v in ipairs(ServerStorage:GetDescendants()) do
		if v.Name == "Bone Throw" then
			Inst = v
		end
	end
end

while true do
	FindNeededItems()
	task.wait(5)
	if not Inst or not RootPart or not Humanoid then return end
	local Attack = Inst:Clone()
	Attack.Parent = game.Workspace
	Attack:PivotTo(CFrame.new(RootPart.CFrame.Position, RootPart.Position+Vector3.new(0, 0, -100)))
	Attack.System["KR Enabled"].Value = true
	Attack.System.Enabled = true
end

Second way:

local RootPart = script.Parent:FindFirstChild("HumanoidRootPart")
local Humanoid = script.Parent:FindFirstChildOfClass("Humanoid")
local ServerStorage = game:GetService("ServerStorage")
local Inst = nil

local function FindNeededItems()
	for i, v in ipairs(ServerStorage:GetDescendants()) do
		if v.Name == "Bone Throw" then
			Inst = v
		end
	end
end

while true do
	FindNeededItems()
	task.wait(5)
	if not Inst or not RootPart or not Humanoid then return end
	local Attack = Inst:Clone()
	Attack.Parent = game.Workspace
	Attack:PivotTo(CFrame.new(RootPart.CFrame.Position, Humanoid.WalkToPoint))
	Attack.System["KR Enabled"].Value = true
	Attack.System.Enabled = true
end

Thanks everybody! @SubtotalAnt8185 was probably the closest to what I needed though, so thanks to you as well. :D

NOTICE: The second method involves something I didn’t include earlier, so it’s not your fault for not mentioning this method!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.