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

Help With Bullet Aiming?

Bullet.CFrame = CFrame.new(Bullet.Position,mouse.Hit.p * CFrame.new(math.random(10), math.random(10), math.random(10)))

I want to bullets from my gun to be slightly inaccurate. The bullets always follow the mouse, but I want them to stray away from the mouse a bit. I tried this but it just resulted in an error.

Bullet.CFrame = CFrame.new(Bullet.Position,mouse.Hit.p) * CFrame.new(math.random(10), math.random(10), math.random(10))

I also tried that but it didn't make it go in a random direction, it instead made it spawn away from the gun pointer.

1 answer

✓ Accepted answer

Please note: This may not work, is currently only theory to me. I also do not know what type of gun this is. Does it have a BodyVelocity or something making it go towards where it is facing?


You could try using math.rad() and CFrame.Angles().

For example;

local angle = CFrame.Angles(
	math.rad(math.random(-20,20)/10),
	math.rad(math.random(-20,20)/10),
	math.rad(math.random(-20,20)/10)
)

Bullet.CFrame = CFrame.new(Bullet.Position,mouse.Hit.p)*angle

To break it down:

CFrame.Angles

Changes where the CFrame is facing, is used like this:

CFrameValue * CFrame.Angles(x,y,z)

It's parameters are in radians, which can be converted from conventional angles using math.rad(angle).

math.random()

Pretty self explanatory, the reason I made it go up to 20 then divide by 10 was because math.random cannot work in decimals.


Hope I helped!

~TDP

1 comment

0

It works just the way I want it to, thanks :D

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