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

[CFrame] Why wont this position CFrame work???

Asked by 9 years ago

Localscript

The bullet wont even shoot from the correct target (EarthPart) in workspace. Why??

local Target = Mouse.Target
    local Dist = ((Target.Position-EarthPart.Position).magnitude)
    local Bullet = Create'Part'{BrickColor=BrickColor.new('New Yeller'),Parent=EarthPart,Name='Bullet',Anchored=true,CanCollide=false,Locked=true,FormFactor='Custom',Size=Vector3.new(.2,Dist,.2)}
    Bullet.CFrame = CFrame.new(EarthPart.Position,Target.Position)
    * CFrame.new(0,-Dist,0)
    local GunSound = Instance.new("Sound",workspace) GunSound.Pitch = 1 GunSound.Volume = 1 GunSound.Looped = false GunSound.SoundId = "rbxassetid://132456235"
    GunSound:Play()
    for i = 0,1.1 do
        Bullet.Transparency = Bullet.Transparency + .1
        Services.run.RenderStepped:wait()
    end
    Bullet.Touched:connect(function(obj)
        if obj.ClassName == 'Model' then
            if obj:FindFirstChild('Humanoid',true) then
                obj.Humanoid.Health = obj.Humanoid.Health - 15
            end
        end
    end)
    Bullet:remove()

2 answers

Log in to vote
5
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
9 years ago

Your mistake

You're setting the initial CFrame of the bullet to the specified relativity of 'EarthPart', which is fine. But when you offset the CFrame with * CFrame.new(0,-Dist,0) then, as you can see, you offset from the Y axis - Y axis is up and down. So since your offset is a negative integer, the bullet will always spawn Dist amount of studs under the original position.


How to fix

So, i'm assuming you want to make the bullet go in front of you, yes? To do this you have to offset from the Z axis - Z axis is front and back, in relative terms. So since the offset is a negative integer, the bullet will now spawn Dist amount of studs in front of the original position.


Code

Bullet.CFrame = CFrame.new(EarthPart.Position,Target.Position) 
                * CFrame.new(0,0,-Dist) --Offset on Z axis(:
0
Thanks! MessorAdmin 598 — 9y
Ad
Log in to vote
3
Answered by 9 years ago

If I'm not mistaken, Z is the front face when working with CFrame.

Bullet.CFrame = CFrame.new(EarthPart.Position,Target.Position) * CFrame.new(0,0,-Dist)

Answer this question