Slamming Creature to Direction w/ Raycasting

Currently I have a system I’m trying to make where it requires you to slam the ghost against a wall and it will have arrows pop up, I’m mainly trying to just get the left side slam to work before I work on the others and I am seeing to have troubles with getting it to work.

I am currently using a raycast from the left of the player’s humanoidrootpart to get the position, and then tweening the ghost to that position, I have been trying to figure out ways to get it directly to the left and not my left and I have been encountering some issues with that, I originally tried raycasting from the ghost’s left, but due to the nature of the system I have it would keep rotating around so I saw it easier and better to raycast from my rootpart.

To sum it up, I’m simply trying to make a system where the ghost is slammed directly to its left and I was wondering what I could use to help fix up my script or adjust in the script to do so, any help will be appreciated. Thank you in advance!

Snippet of code:

local slamEvent = script.Parent.Slam
local tweenService = game:GetService("TweenService")
local slamDebouce = false
slamEvent.OnServerEvent:Connect(function(Player)
	if slamDebouce == false then
		slamDebouce = true
		local filteredParts = Player.Character:GetChildren()
		local rCastOrgin, rCastDirection = Player.Character:WaitForChild("HumanoidRootPart").Position, Player.Character:WaitForChild("HumanoidRootPart").CFrame.RightVector * -50
		local rCastResult = workspace:Raycast(rCastOrgin, rCastDirection)
		local GhostCFrame = hit.Parent:FindFirstChild("HumanoidRootPart").CFrame.LookVector
		local rCastCFrame = rCastResult.Normal
		local playerLV = Player.Character:WaitForChild("HumanoidRootPart").CFrame.LookVector

		originalDistance = hit.Parent:FindFirstChild("HumanoidRootPart").Position
		local tweenInfo = TweenInfo.new(
			0.6,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		local tween = tweenService:Create(hit.Parent:FindFirstChild("HumanoidRootPart"), tweenInfo, {Position = rCastResult.Position - originalDistance })
		local returnTween = tweenService:Create(hit.Parent:FindFirstChild("HumanoidRootPart"), tweenInfo, {Position = originalDistance})
		Player.Character:WaitForChild("Humanoid").WalkSpeed = 0
		tween:Play()
		wait(0.6)
		returnTween:Play()
		wait(0.6)
		Player.Character:WaitForChild("Humanoid").WalkSpeed = 16
		slamDebouce = false
	end
end)

3 Likes

Maybe the issue how you’re calculating the direction for your raycast. You’re using Player.Character:WaitForChild("HumanoidRootPart").CFrame.RightVector * -50 which gives you a direction vector pointing to the player’s left. However, this will always be relative to the player’s orientation, not the ghost’s

If you want the ghost to move directly to its left you should calculate the direction vector based on the ghost’s orientation and no the player lol Try this>>>

local slamEvent = script.Parent.Slam
local tweenService = game:GetService("TweenService")
local slamDebouce = false
slamEvent.OnServerEvent:Connect(function(Player)
	if slamDebouce == false then
		slamDebouce = true
		local filteredParts = Player.Character:GetChildren()
		local GhostCFrame = hit.Parent:FindFirstChild("HumanoidRootPart").CFrame
		local rCastOrgin, rCastDirection = GhostCFrame.Position, GhostCFrame.RightVector * -50
		local rCastResult = workspace:Raycast(rCastOrgin, rCastDirection)

		originalDistance = hit.Parent:FindFirstChild("HumanoidRootPart").Position
		local tweenInfo = TweenInfo.new(
			0.6,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		local tween = tweenService:Create(hit.Parent:FindFirstChild("HumanoidRootPart"), tweenInfo, {Position = rCastResult.Position - originalDistance })
		local returnTween = tweenService:Create(hit.Parent:FindFirstChild("HumanoidRootPart"), tweenInfo, {Position = originalDistance})
		Player.Character:WaitForChild("Humanoid").WalkSpeed = 0
		tween:Play()
		wait(0.6)
		returnTween:Play()
		wait(0.6)
		Player.Character:WaitForChild("Humanoid").WalkSpeed = 16
		slamDebouce = false
	end
end)
2 Likes

I’m aware, that is why I decided to try to the players left. The ghost when in the capture/slamming mode can rotate around and that completely changes the RightVector to being for example ahead of you when it should be going your left.

1 Like

Try this

local slamEvent = script.Parent.Slam
local tweenService = game:GetService("TweenService")
local slamDebouce = false
slamEvent.OnServerEvent:Connect(function(Player)
	if slamDebouce == false then
		slamDebouce = true
		local ghost = hit.Parent:FindFirstChild("HumanoidRootPart")
		local rCastOrgin, rCastDirection = ghost.Position, ghost.CFrame.RightVector * -50
		local rCastResult = workspace:Raycast(rCastOrgin, rCastDirection)

		originalDistance = ghost.Position
		local tweenInfo = TweenInfo.new(
			0.6,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		local tween = tweenService:Create(ghost, tweenInfo, {Position = rCastResult.Position})
		local returnTween = tweenService:Create(ghost, tweenInfo, {Position = originalDistance})
		Player.Character:WaitForChild("Humanoid").WalkSpeed = 0
		tween:Play()
		wait(0.6)
		returnTween:Play()
		wait(0.6)
		Player.Character:WaitForChild("Humanoid").WalkSpeed = 16
		slamDebouce = false
	end
end)
1 Like

this code will assume that hit.Parent is the ghost. If not you’ll need to replace hit.Parent with a reference to the ghost. And please ensure that there are no obstacles between the ghost and the wall that could interfere with the raycast

Check out this thing I wrote a second ago.
Essentially, because raycast takes a direction, you can set the origin to whatever you want, including the ghost’s position. See where I’m getting at? You can use the player’s left direction alright, but you can set the raycast origin as the ghost’s position, and it will always shoot to the left of the cost. Check it out:

local slamEvent = script.Parent.Slam
local tweenService = game:GetService("TweenService")
local slamDebouce = false
slamEvent.OnServerEvent:Connect(function(Player)
	if slamDebouce == false then
		slamDebouce = true
		local filteredParts = Player.Character:GetChildren()
		local rCastOrgin, rCastDirection = hit.Parent:FindFirstChild("HumanoidRootPart").Position, Player.Character:WaitForChild("HumanoidRootPart").CFrame.RightVector * -50
		local rCastResult = workspace:Raycast(rCastOrgin, rCastDirection)
		local GhostCFrame = hit.Parent:FindFirstChild("HumanoidRootPart").CFrame.LookVector
		local rCastCFrame = rCastResult.Normal
		local playerLV = Player.Character:WaitForChild("HumanoidRootPart").CFrame.LookVector

		originalDistance = hit.Parent:FindFirstChild("HumanoidRootPart").Position
		local tweenInfo = TweenInfo.new(
			0.6,
			Enum.EasingStyle.Linear,
			Enum.EasingDirection.Out,
			0,
			false,
			0
		)
		local tween = tweenService:Create(hit.Parent:FindFirstChild("HumanoidRootPart"), tweenInfo, {Position = rCastResult.Position - originalDistance })
		local returnTween = tweenService:Create(hit.Parent:FindFirstChild("HumanoidRootPart"), tweenInfo, {Position = originalDistance})
		Player.Character:WaitForChild("Humanoid").WalkSpeed = 0
		tween:Play()
		wait(0.6)
		returnTween:Play()
		wait(0.6)
		Player.Character:WaitForChild("Humanoid").WalkSpeed = 16
		slamDebouce = false
	end
end)

I just edited the code you put in your first post and made rCastOrigin as the hit.Parent.HumanoidRootPart.Position, so lmk if it errors. Hopefully this helps :smiley:

1 Like

Thank you so much! It did work.

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