Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to change direction of Velocity based on "Accuracy"?

I've made a working Bow tool, which shoots a projectile(arrow) in the direction your mouse is looking. I'd like to add accuracy to the bow, which can be customized. So for example, if the Accuracy is 100, there should be little margin of error, but if the Accuracy is 90, the margin of error increases.

local Accuracy = 100
local arrow = d3.Arrow ---Arrow is a Part located inside "d3" which is a model specified earlier in Script.
			local shot = arrow:Clone()
			local head = char.Head
            shot.CFrame = head.CFrame + mouse.Hit.lookVector * 2 - Vector3.new(0,0.25,0) ---Position Arrow.
			local startPos = shot.Position
			local direction = mouse.Hit.lookVector
            shot.CFrame = CFrame.new( startPos, startPos + direction ) ---Rotates arrow to shoot straight.
			local bod = script.Bod:Clone()
			bod.Force = Vector3.new(0, 196.2, 0) * (shot:GetMass())/1.15 ---Less gravity for flight.
			bod.Parent = shot
			local hurt = script.Killer:Clone() ---Hit detection Script that deals damage.
			hurt.Parent = shot
			hurt.Dmg.Value = damage
			hurt.Disabled = false
			shot.Parent = workspace
			shot.Transparency = 0
			shot.CanCollide = false
			local way = mouse.Hit.lookVector
			shot.Velocity = 130 * way ---This is where I'd like the Accuracy to come into play, the lower the accuracy, the more the arrow can shoot in a direction other than where the mouse is looking.

I hope that all makes sense, thanks for reading.

2 answers

I'm not sure where you would put it in the script, but for this to work, you'll need an 'ending' position. Since I don't think I can find it in your script, this answer will just be for one way of making accuracy, or more on point, in-accuracy.

Note this is the only way I know how to do it, and you may be able to accomplish this with something like CFrame.Angles().

Here is the code:

local accuracyRadius = 1 -- 0 is completely accurate, 0+ is getting more inaccurate

function shoot()
	local x = math.random(-accuracyRadius,accuracyRadius) -- I'm not sure if you can do math.random with negatives. If you can't just put an if statement and another math.random() in there to choose whether it's gonna be negative or positive
	local y = math.random(-accuracyRadius,accuracyRadius)
	local z = math.random(-accuracyRadius,accuracyRadius)
	local endPoint = mouse.Hit.p + Vector3.new(x,y,z)
	-- code
end

Hopefully this will be useful in the future :) If you need anything, post in the comments.

~TDP

2 comments

0

What do you mean I'll need an ending position?

0

Well, you'll need to know where the bullet/arrow will end up for this to work.

After messing around with it for a while, I finally figured out that the problem was that math.random doesn't return decimals, which is very important for tiny changes in accuracy. The script below is an example of how I dealt with that.

			local Accuracy = 95
			local way = mouse.Hit.lookVector
                        local nom = (100-Accuracy)
			local X = math.random(-nom,nom)
			local Y = math.random(-nom,nom)
			local Z = math.random(-nom,nom)
                        local endp = Vector3.new(X/150,Y/150,Z/150)---This creates the needed in-accuracy. 150 can be edited to create more/less accuracy.
                      shot.Velocity = 130 * (way + endp)

This discussion is preserved as it was. New answers, comments, and votes are closed.