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

how do i add cframe.angles to vector3?

Asked by 4 years ago

i need to add a cframe.angles to the player's lookvector so the projectiles has a small amount of spread

local direction = playerCFrame.LookVector
projectile.Position = playerCFrame.Position + direction * PROJECTILE_OFFSET
projectile.Velocity = direction * PROJECTILE_SPEED

if i try adding CFrame.angles(0,math.rad(math.random(-10,10)),0) to direction it comes up with an error. If i just multiply the projectile cframe by the angle then it doesnt work because that doesn't change the direction in which the projectile is traveling in, which is specified on the last line of code.

2 answers

Log in to vote
1
Answered by
Fifkee 2017 Community Moderator Moderation Voter
4 years ago

Hey hey. If you want to convert a Vector3 to a CFrame, you just place the Vector3 into a CFrame, like this:

local Position = Vector3.new(0,5,0);
local CFramePos = CFrame.new(Position);

When given a Vector3, it will automatically cast to the X,Y,Z proportions of the CFrame (because to be honest, they're just Vector3s with extra steps.

With that said, that means you can also add and subtract vectors to CFrames. You cannot subtract and add a CFrame to a Vector3, because that would cause data loss (Vector3s do not have all the rotational properties like how cframe does)

Now, that means you can multiply the character's position (and orientation) by your desired angle by doing something like this:

local Angle = (PlayerCFrame + PlayerCFrame.LookVector * PROJECTILE_OFFSET)  * CFrame.Angles(0, math.rad( math.random(1, 10) ), 0);

If this helped you out a bit, let me know by upvoting and marking this question as an answer. If I screwed up or you have any questions, post a comment. Cheers.

Ad
Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 3 years ago

In order to do this you must first understand what are CFrames.

CFrames is an acronym for Coordinate Frame they describe how a 3D block will act around an object. CFrames can rotate, orientate and even position its self, it's kind of similar to vectors except it can rotate.

If you need more context, visit my other question

What are LookVectors?

LookVectors is a hierarchy of CFrames that represents the surface of a part or unit vector LookVector describes where a front face of a part is interfacing this means that they are 1 stud away from a position.

We can alter a Look Vector by doing this:

part.CFrame = part.CFrame * CFrame.new(0, 0, -5) 
local cf = part.CFrame
local lv = cf.lookVector
part.CFrame = cf + (lv * Vector3.new(5, 5, 5))

This should make the Look vector face -5 on the z-axis.

Also, one quick note you cannot use CFrame.Angles without referencing CFrame.Rad (radians) because CFrames converts angles to radians.

Here's a quick demo of CFrames. Heres a quick demo of LookVector.

0
There is.. so much wrong with this. Fifkee 2017 — 4y
0
I think you meant "math.rad()". youtubemasterWOW 2741 — 4y

Answer this question