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

Can I use Cframe to move the player forward in the direction of the camera?

Asked by
Hero_ic 502 Moderation Voter
9 years ago

Basically what I am trying to do Is now that I have gotten rid of HumanoidController and I decided to start my own HumanoidController" but I do not know if it is possible to use Cframe when key == 'w' so that once the player does this it will move you forward in the camera direction using Cframe. I think this is possible but I do not know for sure. :/

Hope there is some explanation to this.

repeat wait(1/60) until game.Players.LocalPlayer.Character and  
game.Workspace.CurrentCamera
repeat wait(1/60) until game.Players.LocalPlayer.Character:FindFirstChild("Torso")

local Camera = game.Workspace.CurrentCamera
local Torso = game.Players.LocalPlayer.Character.Torso
Camera.CameraType = "Scriptable"
Camera.CameraSubject = nil

repeat wait() until game.Players.LocalPlayer:FindFirstChild("PlayerGui")

local Player = game.Players.LocalPlayer
local size = Instance.new("ScreenGui", Player.PlayerGui)
size.Name = "Size"

local TMaxHeight = 40
local TMinHeight = 10
local TStep = 2.5
local THeight = 20

local Keys = {}

local Mouse = Player:GetMouse()
local mouse = Mouse

local dir = 180
local MI = 0

local Keys = {}

local Locked = false
local MouseTrack = false

function GetRotation(Unit)

    --// Cosine in radian circle: y/x so angle alpha is cos-1(y/x) x=1
    --// Problem here is the circle itself as they have 2 solutions.
    local AbZ, AbX = math.abs(Unit.z), math.abs(Unit.x)
    local Real = math.atan(AbX/AbZ)
    --// ^ugly, super, super, ugly...
    --// Now find the quadrant
    local Plus = 0
    --// Cases are Z and X
    if Unit.z >= 0 and Unit.x >= 0 then 
        --Plus = 0
    elseif Unit.z >= 0 and Unit.x < 0 then 
        Plus = (0.5 * math.pi)
        Real = ((math.pi *1.5) - Real)
    elseif Unit.z < 0 and Unit.x < 0 then 
        Plus = math.pi
    elseif Unit.z < 0 and Unit.x >= 0 then
        Plus = math.pi * 1.5
        Real = ((math.pi * 1.5) - Real)
    end
    return Real + Plus
end     

function Update()
if not Button2Down then
if not Locked and not MouseTrack then

elseif MouseTrack then
    local Size = Player.PlayerGui.Size.AbsoluteSize.X / 2
    local Pos = mouse.X
    local Diff = 0
    if math.abs(Size - Pos) > Size / 20 then
    local this = ((Size - Pos) / (Size))
    local mul = (this < 0 and -1) or 1      
    Diff = math.sqrt(math.abs(this)) * 3 * mul
    end
    dir = dir + Diff
else 
    dir = math.deg(GetRotation(Torso.CFrame.lookVector)) - 90
end
end
local rot = math.rad(dir)
local x = -math.cos(rot)
local z = math.sin(rot)
local New = Vector3.new(x,0,z) / 1000
local this = (mouse.hit.p - Torso.Position ).unit
Camera.CoordinateFrame = CFrame.new(Vector3.new(Torso.Position.x + MI, Torso.Position.y +                   THeight, Torso.Position.z) + New , Torso.Position)
Camera:SetRoll(math.rad(dir))
end


game:GetService("RunService").RenderStepped:connect(function()
Update()
end)

function Zoom(TStep)
    THeight = ((THeight + TStep <= TMaxHeight and THeight + TStep >= TMinHeight) and THeight +      TStep) or THeight
end

mouse.WheelBackward:connect(function()
    Zoom(TStep)
end)

mouse.WheelForward:connect(function() Zoom(-TStep) end)

mouse.KeyDown:connect(function(key)
    if key == "i" then
        Zoom(-TStep)
    end
    if key == "o" then
        Zoom(TStep)
    end
    if key == "d" then
        dir = dir + 1
    end
    if key == "a" then
        dir = dir - 1
    end
end)
mouse.KeyUp:connect(function(key)
    Keys[key] = false
end)
0
Use the lookVector CFrame constructor of the CurrentCamera's CoordinateFrame Property. Goulstem 8144 — 9y
0
Sorry i'm kinda new to coding so I need a bigger explanation? Hero_ic 502 — 9y
0
Can't you use the Move method of the Humanoid to do what you're doing? http://wiki.roblox.com/index.php?title=API:Class/Humanoid/Move Spongocardo 1991 — 9y

3 answers

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

What You Need To Do

You need to CFrame the Character's Torso relatively to itself, but with an offset of however much you want to move them multiplied by the lookVector of the Camera's CoordinateFrame.

Code

Expected to be in a LocalScript

wait(1)

local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local tor = plr.Character:WaitForChild('Torso')

--Relatively to itself
tor.CFrame = tor.CFrame
            --How much you want to move them
            * CFrame.new((CFrame.new(0,0,-1)
            --Multiplied by lookVector of the Camera's CoordinateFrame
            * cam.CoordinateFrame.lookVector * 1))
Ad
Log in to vote
1
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

You can always use the Move method of Humanoid.

Move (Vector3 moveDirection, bool relativeToCamera)

The first argument is just the the Vector3, or 3D point in the place, or position you want your *humanoid *(player) to head towards until interrupted by the pressing of keys or a certain object

The second argument is set to false by default setting it to true would mean allowing the movement of the player to in relation or proportion to the Camera

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild('Humanoid')

humanoid:Move(Vector3.new(45, 0, 45), true)
Log in to vote
0
Answered by
iLegitus 130
9 years ago

Well,Im getting it that you want the player to move into the direction the camera is focusing on.

Well,A decent explenation is,Just make the camera move/focus on whatever brick you want,And then make another part that you want the player to walk to and use the :MoveTo() on the player's humanoid (MoveTo() to the position of the part you want the player to walk into),But be sure to disable the player's control or just while true do it and then break the code once the player is there.

Answer this question