Spherecast not going to the right direction?/Part's LookVector is not changing to character's LookVector?

Hello. I am trying to script a shuriken throw skill by using Spherecast for hit detection.
The problem is that even though I am adjusting the CFrame and LookVector of the shuriken (and I am shooting the Spherecast from part to its LookVector * 5) it only works accurately from one side. So what I am thinking is that I am not adjusting the LookVector correctly. I want to make it so that the shuriken’s LookVector will be wherever player was looking at before throwing it.

Here is the script:

		local castParams = RaycastParams.new()
		castParams.FilterType = Enum.RaycastFilterType.Exclude
		castParams.FilterDescendantsInstances = {player.Character, workspace.Debris}

		local projectile = RS.Clans.Uhkia.Sakusa.SakusaSkills.NinjaStar:Clone()
		projectile.Parent = debris
		projectile.CFrame = character.RightHand.CFrame
		projectile.CFrame = CFrame.lookAt(projectile.Position, humRP.CFrame.LookVector)
		projectile:SetNetworkOwner(player)
		
		local projectileSphere = RS.Clans.Uhkia.Sakusa.SakusaHitboxes.NinjaStarSphereCast
		
		local projectileForce = Instance.new("BodyVelocity")
		projectileForce.MaxForce = Vector3.new(1e6,1e6,1e6)
		projectileForce.Velocity = humRP.CFrame.LookVector * 50
		projectileForce.Parent = projectile		
	
		local hasHit = false
		local direction = projectile.CFrame.LookVector * 5
		
		while hasHit == false do
			wait()
			local results = workspace:Spherecast(projectile.CFrame.Position, projectileSphere.Size.X/2, direction, castParams)
	
			if results then
				hasHit = true
				print(character.Name.." hit with shuriken")
				projectile:Destroy()
				

				
			end
			
		end

Edit: What am I doing wrong here?

3 Likes

Hi, I’ve never seen Spherecast’s setup before so if you don’t mind me asking, what is the 2nd parameter in the cast for?

1 Like

The radius of the sphere that will be casted

1 Like

More info here

1 Like

When you set the projectile’s CFrame using CFrame.lookAt, you’re actually pointing the projectile towards the origin instead of the front direction of the humanoid root part.

The way CFrame.lookAt works is that it’ll create a CFrame at the first position which faces towards the second position. The LookVector of a CFrame is somewhere around the origin which is why this seems to only work in one direction.

To get the projectile to shoot forward from the character regardless of where they are and what direction they’re facing, you’ll have to transform the position you want the CFrame to look at to be relative to the first argument.

This can be done by doing this:

CFrame.lookAt(projectile.Position, projectile.Position + humRP.CFrame.LookVector)

What this does is that it takes the position that the LookVector would be and instead of having it be around the origin, it’ll be around the projectile position. This will provide a correct LookVector that’s relative to the direction that the player is facing.

2 Likes

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