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

How to make a rotated velocity?

Let's say you have an horizontal object constantly shooting another object horizontally at a constant velocity.

It's easy to make this, as it corresponds likely with the coordinate axis.

Projectile.Velocity = Vector3.new(ShotVelocity, 0, 0);

Now let's say you wanted to rotate the object upwards so that it would shoot projectiles out at the same velocity but at a different angle. Let's say it's 15 degrees higher than the horizontal

Projectile.Velocity = Vector3.new(ShotVelocity*math.cos(math.rad(15)), math.abs(Shot.Velocity*math.sin(math.rad(15)),0))

Given this general relationship, how would you model a brick that shoots objects out at a relative direction it is facing using a consistent velocity? By relative direction, I specify using the part's Rotation Property.

1 answer

✓ Accepted answer

As for 2 dimensional vectors, it seems like you already know how to do that. But 3 dimensional vectors are really complicated, so you probably don't want to do that manually. Just use some sort of unit vector from a part's CFrame, and multiply by the desired velocity.

Here's a cannon as an example.


local angle=60 --degrees
local speed=100

local cannon = Instance.new('Part')
cannon.Name='Cannon'
cannon.Anchored=true
cannon.Size=Vector3.new(7, 1, 2)
cannon.CFrame=CFrame.new(0,5,-10)*CFrame.Angles(0,0,math.rad(angle))
cannon.Parent=workspace

local ball = Instance.new('Part')
ball.Name='Ball'
ball.Shape=Enum.PartType.Ball
ball.Size=Vector3.new(2, 2, 2)
ball.Parent=game.ServerStorage

while wait(3) do
    local b = ball:Clone()
	local p = cannon.CFrame.p
    local dir = (cannon.CFrame*CFrame.Angles(0,0,math.rad(-90))).upVector
    local target = p + dir*500
    b.CFrame=CFrame.new(p+dir*(cannon.Size.X/2), target)
    b.Velocity=dir*speed
    b.Parent=workspace
	b.Touched:connect(function(hit)
		if hit~=cannon and hit.CanCollide then
			Instance.new('Explosion',workspace).Position=b.Position
			b:Destroy()
		end
	end)
end

4 comments

0

Thankn you so much for this alternative answer - though I can't say I understand the CFrame operations performed. Is there a tutorial on CFrame operations that you could link me to? The Roblox Wiki one is a bit to much for me.

0

when I create "cannon" I just multiply by CFrame.Angles, which is similar to using the "rotation" property. When I get "dir": I get the direction the cannon is facing with .upVector. But I have to rotate the cframe again because directions normally face out of the side, not the top like I want. for target I use dir like a ray to create a theoretical position that the cannon is looking at. Of cours

0

e dir*speed is simply velocity.

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