i want part 1 to face the front surface of part 2
Alright so you need to use CFrames in order to do this
local NewCFrame = CFrame.new(Part1.Position, Part2.Position) -- this will create a new CFrame
Part1.CFrame = NewCFrame -- sets Part1's CFrame to the new CFrame we just created
Note: You also could find this on google.
I’m pretty sure his saying that he wants the part1’s front surface to face the front surface of the part2.
Here’s an example:
That’s what I did in those lines of code.
That’s not the expected behaviour of this code, as this code only makes the part’s front surface face the target’s position – which is the centre. This can be used:
local target = Part2.CFrame * CFrame.new(0, 0, -Part2.Size.Z / 2)
Part1.CFrame = CFrame.new(Part1.Position, target.Position)
https://gyazo.com/0642775f2b38a853cdd2c94c5143d558
this is the result from using your code
why is it rotated like that?
Alternatively, you could use the PointToWorldSpace function and feed if the LookVector. This would give you a precise target translated in relation to the CFrame’s rotation.
ie.
local target = part1.CFrame:PointToWorldSpace(part1.CFrame.LookVector);
part2.CFrame = CFrame.lookAt(part2.Position, target);
FYI: CFrame.new(position, lookAt)
was superseded by CFrame.lookAt()
, it just remains for backwards compatibility.
What is part1 being defined as? They all seem to face the same way.
im confused so heres the code:
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player, pos, target) -- First return in the player, then any passed values
local part = game.ReplicatedStorage.ball:Clone()
local target2 = target.CFrame:PointToWorldSpace(target.CFrame.LookVector);
part.CFrame = CFrame.lookAt(part.Position, target2);
part.Target.Value = target
part.Parent = game.Workspace
part.Position = pos
end)