How would I move one object closer to another using a script?

I’m trying to move a knife object closer to the object it hit by 0.25 studs, but I can’t find out how. I’ve tried moving it relative to the hit object, raycasting, etc.

Here’s what I have so far:

knife.Touched:Connect(function(hit)
	if hit.Parent ~= char and hit ~= knife and hit ~= handle and hit.Parent.Parent ~= char then
		knife.Anchored = true
		keepSpinning = false
		bv:Destroy()
		knife.CFrame = CFrame.lookAt(knife.Position, direction)
		knife.Orientation = Vector3.new(knife.Orientation.X + 240, knife.Orientation.Y, knife.Orientation.Z)
							
		--Changing knife position here
	end
end)

Basically I want to find out which way I need to move my knife for it to be closer to the object that it hit.

4 Likes

You could try using Magnitude.

1 Like

How would I do this? Could you give an example?

1 Like
print((workspace.Part1.Position- workspace.Part2.Position).Magnitude)

image

2 Likes

Yes that gives the distance between the two parts, but how would I use this to move my object closer to the other?

I don’t know if this will work but try this:

knife.CFrame = CFrame.lookAt(knife.Position, direction)
knife.Orientation = Vector3.new(knife.Orientation.X + 240, knife.Orientation.Y, knife.Orientation.Z)

local dist = (knife.Position - (your object name here).Position).Magnitude
knife.CFrame = (your object name here).CFrame * CFrame.new(0,0,-dist)
3 Likes

It’s not working very well. I’ll update the post with a larger portion of the script so you can see better.

2 Likes

Try this I guess

knife.Touched:Connect(function(hit)
	if hit.Parent ~= char and hit ~= knife and hit ~= handle and hit.Parent.Parent ~= char then
		knife.Anchored = true
		keepSpinning = false
		bv:Destroy()
		knife.CFrame = CFrame.lookAt(knife.Position, direction)
		knife.Orientation = Vector3.new(knife.Orientation.X + 240, knife.Orientation.Y, knife.Orientation.Z)
							
		knife.CFrame = hit.CFrame * CFrame.new(0,0,hit.CFrame.Z / 2)
	end
end)

If It goes further and not back then try this:

knife.Touched:Connect(function(hit)
	if hit.Parent ~= char and hit ~= knife and hit ~= handle and hit.Parent.Parent ~= char then
		knife.Anchored = true
		keepSpinning = false
		bv:Destroy()
		knife.CFrame = CFrame.lookAt(knife.Position, direction)
		knife.Orientation = Vector3.new(knife.Orientation.X + 240, knife.Orientation.Y, knife.Orientation.Z)
							
		knife.CFrame = hit.CFrame * CFrame.new(0,0,-hit.CFrame.Z / 2)
	end
end

2 Likes

The first one moved too far, the second moved too far inward. This is the second one: 2023-08-23 11-30-38

1 Like

I can see problem properly now, so you want that whenever you throw knife it stays with hit?

1 Like

This is what happens without moving it: 2023-08-23 11-35-08
It already stays where the hit is, though I need to move it about 0.25 studs closer to the hit object for it to look like the knife actually impaled the object.
By the way thank you for all of the help so far.

1 Like

Your welcome
What if you try this?

knife.Touched:Connect(function(hit)
	if hit.Parent ~= char and hit ~= knife and hit ~= handle and hit.Parent.Parent ~= char then
		knife.Anchored = true
		keepSpinning = false
		bv:Destroy()
		knife.CFrame = CFrame.lookAt(knife.Position, direction)
		knife.Orientation = Vector3.new(knife.Orientation.X + 240, knife.Orientation.Y, knife.Orientation.Z)

if knife.Position.Y >= 1 then
	knife.CFrame = hit.CFrame * CFrame.new(0,0.25,0.25)
elseif knife.Position.Y < 1 then
	knife.CFrame = hit.CFrame * CFrame.new(0,-0.25,0.25)
end
	end

end

I’ve tried something like this before. It just ends up teleporting the knife to the center of the hit object, since your setting it to the hit objects CFrame. I just tried this and it worked somewhat well:

knife.Position = endPos
--endPos is the position of the mouse when the knife was thrown

The only problem with this is that it’s somewhat inconsistent, though I could use it for now until I find any bugs with it.

I thought about that before, but I thought you was scripting in ServerScript and not in LocalScript, so uh I guess I couldn’t help at all, well fortunately you figured out how to do it

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