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

How can I rotate the lookvector of a part?

Asked by
Mystdar 352 Moderation Voter
8 years ago

I have made a gun like script to fire a projectile and I came across this logic error. The part is facing the direction it is traveling just at the wrong orientation, the wrong side is facing the direction of travel. This picture shows the syringe's point facing the ground with the side of the syringe facing the point. I want the point of the projectile facing the direction of travel. This is the positioning code I used.

local spawnPosition = (Right.Doctor.Syringe.CFrame * CFrame.new(2, 0, 0)).p
bullet.CFrame = CFrame.new(spawnPosition, mouse.hit.p) -- This is the positioning line I have set it to 
bullet.Velocity = bullet.CFrame.lookVector * 60 -- 60 is speed
bullet.Parent = game.Workspace

On line 2 I have set it to face the position the mouse was at when the mouse clicked. I have tried changing the orientation with radians, but that hasn't works so far, yet I am fairly new with radians, so I may be using them inaccurately. This was my raidan attempt:

bullet.CFrame = CFrame.new(spawnPosition, mouse.hit.p*(1/2*math.pi))

However there was no change in rotation of the syringe(bullet)

2 answers

Log in to vote
1
Answered by
1waffle1 2908 Trusted Badge of Merit Moderation Voter Community Moderator
8 years ago

You want to rotate the part correctly by multiplying its CFrame by some rotation:

local spawnPosition=(Right.Doctor.Syringe.CFrame*CFrame.new(2,0,0)).p 
local cframe=CFrame.new(spawnPosition, mouse.hit.p)
bullet.CFrame=cframe*CFrame.Angles(math.pi/2,0,0)
bullet.Velocity=cframe.lookVector*60
bullet.Parent=workspace
Ad
Log in to vote
2
Answered by
Spectrobz 140
8 years ago
bullet.CFrame = CFrame.new(spawnPosition, mouse.hit.p) * CFrame.Angles()
bullet.Velocity = (bullet.CFrame * CFrame.Angles()).lookVector * 60 

You can try messing up with the angles this way

0
Got this error: attempt to multiply a Vector3 with an incompatible value type or nil Mystdar 352 — 8y
0
Why are you multiplying by `CFrame.Angles()`? That does nothing. BlueTaslem 18071 — 8y
0
You actually put numbers the rotation you need ... Spectrobz 140 — 8y

Answer this question