So I currently have a tween here that will rotate a part and face another part. Also move it’s CFrame towards the part2’s position and also raise it’s Y axis a bit to account for it not going into the ground. Anyway, my issue right now is. This works. But how can I make the part stop 100 studs in front of part2’s position?
I tried this:
local goal = (part2.CFrame - (part2.CFrame.lookVector * 100) + Vector3.new(0,part1.CFrame.Y,0)) * CFrame.fromEulerAnglesXYZ(0, y, 0)
But then the rotation gets all messed up, can anyone help me what I’m doing wrong with this. Main code:
local tweenService = game:GetService("TweenService")
local part1 = script.Parent
local part2 = game.Workspace.FacePart -- The part that part1 will face
local rotation = CFrame.lookAt(part1.Part.Position, part2.Position)
local x,y,z = rotation:toEulerAnglesYXZ()
local goal = (part2.CFrame + Vector3.new(0,part1.Part.CFrame.Y,0)) * CFrame.fromEulerAnglesXYZ(0, y, 0)
local info = TweenInfo.new(4, Enum.EasingStyle.Linear) -- Tween for 4 seconds with linear easing
local tween = tweenService:Create(part1.Part, info, {CFrame = goal})
tween:Play()
I’m not sure you understood my question. I need to calculate the angle of CFrame 100 studs from it’s target.
Right now I’m using this below. But as soon as I add this .lookVector bit into the mix, the rotation goes out of whack. So I’m not sure how to get the angle as well as calculate distance.
local goal = (part2.CFrame - (part2.CFrame.lookVector * 100) + Vector3.new(0,part1.CFrame.Y,0)) * CFrame.fromEulerAnglesXYZ(0, y, 0)
i am crying after reading this post, i dont understand any sentence you say, you didnt provide an image example, a video example. I lost my house and my job after reading this post.
local tweenService = game:GetService("TweenService")
local part1 = script.Parent
local part2 = game.Workspace.FacePart -- The part that part1 will face
local rotation = CFrame.lookAt(part1.Part.Position, part2.Position * 100)
local x,y,z = rotation:ToEulerAnglesXYZ()
local goal = (part2.CFrame + Vector3.new(0,part1.Part.CFrame.Y,0)) * CFrame.Angles(0, y, 0)
local info = TweenInfo.new(4, Enum.EasingStyle.Linear) -- Tween for 4 seconds with linear easing
local tween = tweenService:Create(part1.Part, info, {CFrame = goal})
tween:Play()
1 part I want to move, and another that is the destination part.
This script currently tweens (Part1) rotation to face towards the destination part (Part2), and then also move (Part1) towards (Part2)
This works. But it will basically go all the way until it collides with Part2. I want it to stop 100 studs before it reaches Part2. I tried doing this with my lookVector snippet above ^ but this seems to mess with it’s rotation.
I see what you mean, first we have to to get the direction from point A (Part1) to point B (Part2) like this:
local rel_pos = Part2.Position - Part1.Part.Position
And lastly, we multiply the rel_pos unit by -100 and add this to the original position of Part2.
local dist_from_target = rel_pos.unit * -100
local target_pos = Part2.Position + dist_from_target
The final code should look like this:
local tweenService = game:GetService("TweenService")
local part1 = script.Parent
local part2 = game.Workspace.FacePart -- The part that part1 will face
local rel = part2.Position - part1.Part.Position
local dist_from_target = rel.unit * -100
local target_pos = part2.Position + dist_from_target
local rotation = CFrame.lookAt(part1.Part.Position, part2.Position)
local x,y,z = rotation:toEulerAnglesYXZ()
local goal = CFrame.new(target_pos + Vector3.new(0,part1.Part.CFrame.Y,0)) * CFrame.fromEulerAnglesXYZ(0, y, 0)
local info = TweenInfo.new(4, Enum.EasingStyle.Linear) -- Tween for 4 seconds with linear easing
local tween = tweenService:Create(part1.Part, info, {CFrame = goal})
tween:Play()
Looks to me like it’s facing the same direction as the part. I don’t see the issue here. The improved script seems like it should be working fine. Did you mark the front face of the part with anything for testing?
To have the parts face the same direction (Adjust the offset if needed)
Script
local tweenService = game:GetService("TweenService")
local part1 = script.Parent
local part2 = game.Workspace.FacePart -- The part that part1 will face
local yOffset = 90
local rotation = CFrame.Angles(0, math.rad(part2.Rotation.Y + yOffset), 0)
local goal = CFrame.new(Vector3.new(part2.CFrame.X,part2.CFrame.Y, part2.CFrame.Z)) * rotation
local info = TweenInfo.new(4, Enum.EasingStyle.Linear) -- Tween for 4 seconds with linear easing
local tween = tweenService:Create(part1, info, {CFrame = goal})
tween:Play()
To have the tank part face towards the goal part:
Script
local tweenService = game:GetService("TweenService")
local part1 = script.Parent
local part2 = game.Workspace.FacePart -- The part that part1 will face
local yOffset = 0
local x,y,z = CFrame.lookAt(part1.Position, part2.Position):ToEulerAnglesXYZ()
local goal = CFrame.new(part2.Position) * CFrame.fromEulerAnglesXYZ(0, y, 0)
local info = TweenInfo.new(4, Enum.EasingStyle.Linear) -- Tween for 4 seconds with linear easing
local tween = tweenService:Create(part1, info, {CFrame = goal})
tween:Play()