Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Variate Directions Based Off CFrame Vectors?

Asked by
b2lego 30
4 years ago

Hi, everyone!

I'm very new at studio but I think I'm beginning to get the hang of it. So since I'm new I've made some simple stuff and I wanted to advance by making a simple firearm. I've encountered a small problem being that every time a round is fired, a cartridge (a small yellow cylinder) has to fling out the right side of the weapon. I've managed to do this by spawning the cartridge with the velocity being the rightVector of the gun's CFrame as shown below.

local Gun = game.Workspace.Gun

function createCartridge()
    local newCartridge = Instance.new("Part")
        --code
    newCartridge.Velocity = Gun.CFrame.rightVector * math.random(35,65)
        --code
end

While this works just fine, it looks like the cartridge follows a very "restrictive" trajectory with the only variation being the speed at which the shell is ejected. My question is how would I be able to add some variation on the Z and Y axis based off the the rightVector to make it appear as though the Cartridge is flying randomly to the general right hand side relative to the gun.

I've tried multiplying 2 different CFrame Vectors

Gun.CFrame.rightVector * Gun.CFrame.upVector * 50

That......didn't work and I'm sure that was a laughable mistake....

I tried making another part that moved to the gun but was rotated so that that rightVector was actually facing up but I was sure there was a more efficient method

I looked around on google and this forum but I'm not quite sure how to describe my question aside from diagonal cframe....which didn't turn out a very prudent search...

If anyone could show me how to do that or give me an idea, I would appreciate that very much as I think I'll be using it a lot in the future for many things!

Sidenote: I didn't really know how to describe the issue so if someone could refer me to a resource, I'll delete this post

1 answer

Log in to vote
0
Answered by
b2lego 30
4 years ago

Ok I've figured it out, you will have to add some of the different vectors and then apply a coefficient to them for example,

Cartridge.Velocity = (Cartridge.CFrame.rightVector + Vector3.new(0,(math.random(25,50))/100,0) + Cartridge.CFrame.lookVector * (math.random(-100,100)/100)) * math.random(25,50)
Ad

Answer this question