Apply impulse projectile deviates slightly from target position

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?

I am trying to get the player to throw the sword and the sword is supposed to go to where the mouse is pointed at.

  1. What is the issue?

Whenever I test it, it seems as if the sword deviates and goes towards slightly off from the target position.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I thought it was the rotation problem but it is not because even if I get rid of the rotation it is still not accurate. I used ApplyImpulse and ApplyAngularImpulse to add force to my object. Is there something that I’m doing wrong? This is my first time creating a projectile so any feedback would be appreciated.

Server side code:

elseif abilityName == "Throwable" then
		
		-- play the throwing animation
		local throwingTrack = animator:LoadAnimation(throwingAnim)
		throwingTrack:Play()
		
		wait(0.5) -- wait for the animation to finish playing		
		
		handle.Transparency = 1  -- handle already defined and it is the weapon model
		-- sets the children of the weapon to transparent
		setMeshChildrenTransparency(handle,1)
		
		-- create the weapon model
		local projectile:MeshPart = handle:Clone()
		projectile.Blade.CanTouch = true
		projectile.Parent = workspace
		
		-- apply force and torque to the projectile 
		projectile.CFrame = CFrame.new(projectile.Position,mousePos.LookVector)
		projectile:ApplyImpulse(Vector3.new(0,3,0))
		projectile:ApplyImpulse(mousePos.LookVector * projectile.AssemblyMass * 300) --translational velocity
projectile:ApplyAngularImpulse(projectile.HitBox.CFrame:VectorToWorldSpace(Vector3.new(-75,0,0)) -- angular velocity
			--*projectile.AssemblyMass)

		-- firing all clients to create a copy of the projectile to all clients for smooth gameplay
		TW:FireAllClients(projectile,weapon.Parent)
		
		projectile.Blade.Touched:Connect(function(touched:BasePart) 
			-- checks if the projectile is touching itself, or is touching the charater of the player who threw the weapon,
			-- or if it is touching the
			if touched:IsDescendantOf(projectile) or char:IsAncestorOf(touched) or touched.Transparency == 1 then return end
			
			-- stop projectile from moving
			projectile.Blade.CanTouch = false 
			
			local humanoid = touched.Parent:FindFirstChildOfClass("Humanoid")
			-- damage the humanoid that was hit if it was a humanoid
			if humanoid then
				humanoid.Health -= weapon:GetAttribute("Damage")*1.25
				local weld = Instance.new("WeldConstraint")
				weld.Part0 = projectile
				weld.Part1 = touched
				weld.Parent = projectile
			else
				projectile.Anchored = true 
			end
		end)
		
		wait(10)
		
		handle.Transparency = 0
		setMeshChildrenTransparency(handle,0)
	end

LocalScript code:

TW.OnClientEvent:Connect(function(weapon:MeshPart,char)
	-- does the same thing on localscript
	local weaponClone = weapon:Clone()
	weaponClone.Parent = workspace
	weaponClone.Transparency = 0 
	setMeshChildrenTransparency(weaponClone,0)
	
	weapon:Destroy() -- serverside projectile is not needed on client side, so destroy
	
	weaponClone.Blade.Touched:Connect(function(touched:BasePart)
		-- the same thing from server script
		if touched:IsDescendantOf(weaponClone) or char:IsAncestorOf(touched) or touched.Transparency == 1 then return end
		
		local humanoid = touched.Parent:FindFirstChildOfClass("Humanoid")
		
		if humanoid then
			local weld = Instance.new("WeldConstraint")
			weld.Part0 = weaponClone
			weld.Part1 = touched
			weld.Parent = weaponClone
		else
			weaponClone.Anchored = true
		end
		
		print("on client, touched " .. touched.Name)

	end)
end)

I wouldn’t use ApplyImpulse tbh. Try using AssemblyLinearVelocity instead.

I asked Bing AI to make a part that spawns on an imaginary circle that is closest to the direction of the mouse, and it wrote the code for that exact thing.

I basically used what it wrote for spawning the weapon model at the circle position and the accuracy improved of the projectile improved greatly.

-- mousePos is a CFrame
local rootPart = char:FindFirstChild("HumanoidRootPart")
local mouseDirection = (mousePos.Position - rootPart.Position).Unit
local partPosition = rootPart.Position + Vector3.new(mouseDirection.X, 0, mouseDirection.Z) * 10 -- radius of circle
projectile.CFrame = CFrame.new(partPosition,mousePos.LookVector)

But I was wondering why is it better to use AssemblyLinearVelocity instead of AppyImpulse()?
Does it have any advantages over ApplyImpulse?

It was just something to try, I have never actually seen ApplyImpulse before lol.

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