Help with lookvector and CFrame

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()

you can use math.min to limit the amount that it can go to, so for example:

local limit = math.min(100, ZPosition) -- depending on which one is the lowest, it will determine what number it is

you can also use math.clamp() to add a min and max to both sides:

local limit = math.clamp(ZPosition, -100, 100) -- This will limit the ZPosition to be between these 2 numbers

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()








A bit dramatic.

I have 2 parts.

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.

Without my look vector snippet: https://gyazo.com/9b01c480ca6439ad2f775d6c29ba85cb
Properly faces and moves towards direction of Part2 (destination part)

With my look vector code: https://gyazo.com/298b0a29520e3e8db4dcf9866332b97d
Steers clear of 100 studs, but the rotation gets messed up now.

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()

I haven’t tested it, so it might not work.

Ah rotation appears to be completely messed up now

https://gyazo.com/6abc2da51f8d07f06c885effaa0f333d

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?

How is it facing the same direction? lol can you see the tank model. Ideally the gun should be facing the part.

My script above ^

local goal = (part2.CFrame + Vector3.new(0,part1.Part.CFrame.Y,0)) * CFrame.fromEulerAnglesXYZ(0, y, 0) 

This code currently works and will tween both rotation and position of the CFrame towards the destination part. “part2”

If you add a decal to the front of the part it should show on one of the long sides of the part. Does it not?

Yea it looks like this:

https://gyazo.com/1dd1e1cd1a02f2257c084c74e6a60373

But how come my script moves the part and rotation facing the part2?
Is it actually wrong but the tank mesh is just oriented differently?

Want to give this a go?

local goal = CFrame.lookAt(part2.Position + (part1.Position-part2.Position)*100, part2.Position)

You should be able to get rid of rotation if this works.

No shot sadly, it zoomed off into oblivion

My bad, I forgot .Unit.

local goal = CFrame.lookAt(part2.Position + (part1.Position-part2.Position).Unit*100, part2.Position)

Are either of these what you wanted?

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()