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

What's the diffrence between these two lines?

Asked by 4 years ago
Edited by Ziffixture 4 years ago

Hello i'm new to scripting. Was trying to make a script that would make the player face the camera's look vector. Found a lot of examples online but this one was the most simplest for me. however i'm still confused. Was wondering about the difference between those two lines below? why did "root,CFrame.P" get assigned the second time inside the vector in example 1? Most importantly why is example 2 incorrect? Thank u

----Example 1 (correct one)


local cam = workspace.CurrentCamera local plr = game:GetService("Players").LocalPlayer local root = plr.Character.HumanoidRootPart game:GetService("RunService").RenderStepped:connect(function() if plr.Character then root.CFrame = CFrame.new(root.CFrame.p,root.CFrame.p+Vector3.new(cam.CFrame.lookVector.X,0,cam.CFrame.lookVector.Z)) end end)

----Example 2 (Wrong one)


local cam = workspace.CurrentCamera local plr = game:GetService("Players").LocalPlayer local root = plr.Character.HumanoidRootPart game:GetService("RunService").RenderStepped:connect(function() if plr.Character then root.CFrame = CFrame.new(root.CFrame.p, Vector3.new(cam.CFrame.lookVector.X,0,cam.CFrame.lookVector.Z)) end end)

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

The lookVector returns the unit of the CFrame's facing direction relative to the world. In other words, it will return the return the position 1 stud away from Vector3.new(0, 0, 0) in the direction the CFrame is looking.

So if the Player is looking East, the lookVector will be Vector3.new(1, 0, 0)

When you define your CFrame in your script, you are using:

CFrame.new(Position, DirectionToLook)

DirectionToLook is relative to the Player, whereas a lookVector is relative to the world, so when you only put the lookVector as the DirectionToLook in then you are telling the new CFrame to look in the direction of Vector3.new(1, 0, 0) which would be towards the centre of the map. In the correct example, the lookVector is added to the position vector so it is looking 1 unit away from the HumanoidRootPart in the direction the HumanoidRootPart is facing.

Sorry for the poor explanation, it's a difficult one to get your head around without visual examples.

0
Thank you so much! But wouldn't problem 2 be solved/valid  if i were to use " :ToObjectSpace() " when setting the camera's value then? if so how would u use it? Thank u in advance         davie10863 9 — 4y
0
Hey! thank u so much for helping me! I really appreciate it and it means a lot to me, i mean it! i hope you have a good rest of your day and blessings come your way! davie10863 9 — 4y
0
You'd want to use :toWorldSpace in this example, root.CFrame:toWorldSpace(CFrame.new(cam.CFrame.lookVector)) [I think] darkelementallord 686 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

Hello . To answer my own question, Yes it would fix problem number 2 if i were to do just that. Here is the script:

local cam = workspace.CurrentCamera
local plr = game:GetService("Players").LocalPlayer
local root = plr.Character.HumanoidRootPart
game:GetService("RunService").RenderStepped:connect(function()
    if plr.Character then
        local lol = root.CFrame:ToObjectSpace(cam.CFrame)
        root.CFrame = root.CFrame * CFrame.Angles(0, -lol.LookVector.X, 0)
    end
end)
  • Thank u so much for the answers!! Much appreciated!

Answer this question