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

How would you convert a rotation to a Vector3 velocity?

Asked by
drew1017 330 Moderation Voter
8 years ago

This sounds like a request but im completely stumped here so bear with me, the wiki gives no information on how to do this.

Basically what I mean is something like old-school-roblox guns that fired spheres in the direction of the mouse, which you'd need rotation to compute. Whenever I tried to look at some of them and see the firing code, all it was about the velocity was something like ''v * 200'' and I couldnt find any way on how they could factor rotation into a velocity, as im trying to set the velocity of a projectile in my game based on the rotation of the torso.

Could anyone explain how to do this?

0
i edited my post Perci1 4988 — 8y

1 answer

Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
8 years ago

You're partially right. You do need to rotate the bullet, but it's not nearly so difficult as you seem to think.

Basically, we need to make the bullet face the mouse. Then, since it's already facing the correct direction, we can just make the bullet fly forward.

To make it face the mouse, we can use the CFrame.new(Vector3 position, Vector3 lookAt) constructor. We want it to start at the Handle, and look at the mouse. Therefore we must do something like;

bullet.CFrame = CFrame.new(handle.Position, mouse.Hit.p)

Now to make it fly forward. For this we can use CFrame's lookVector property, which is the direction the Part is facing. 'Forward' is determined by the front face. So we just set the Part's Velocity to this, times some large number to give it more speed.

bullet.Velocity = bullet.CFrame.lookVector * speedNumber

But this probably won't make the bullets fly very well. Most guns need some sort of BodyMover object. If you use the Velocity property, then you'll probably need to add a BodyForce counteracting gravity to keep the bullet in the air. Instead of using the property, however, you can also use a BodyVelocity, although last time I checked there was no gravitational effects when using it.



Removing the Y is bit more complicated. We need to start by getting the direction between the mouse and the handle. The standard form for this is to - from, and we're going to the mouse, so this should work:

direction = (mouse.Hit.p - handle.Position)

But how does this give us the direction? This confused me a lot at first, but what you have to do is simplify everything in your brain. Let's say you want to get the direction between point A and point B. A is at 0, 5, 0 and B is at 0, 15, 0. We're at A, we want to get to B. Therefore, we must do B - A.

This gives us 0, 10, 0. Now wait a second. If we're at A, and we want to go to B, wouldn't that mean that we would go up 10 studs? Yes, it would, and that's the exact answer we got from subtracting. Adding in the X and Z is the same, just more complex.


So far, we have the direction, but it will still affect the Y. To fix this, we just need to multiply the Y by 0. Everything times 0 equals 0.

direction = (mouse.Hit.p - handle.Position) * Vector3.new(1, 0, 1)

Now we just add the direction to the Handle's position, and we're good to go.

bullet.CFrame = CFrame.new(handle.Position, handle.Position + direction)
0
This really helped, but do you think you could figure out how to do this on only certain axises on mouse.hit.p (I.e. it will work as this does but will only change rotation on x and z axis, as im making a top-down game)? drew1017 330 — 8y
0
Everything works, you've helped a ton. drew1017 330 — 8y
Ad

Answer this question