How to adjust camera lookvector horizontally?

I have made a dash script that uses the camera.cframe.lookvector value so when the player dashes, their body follows the camera:

https://gyazo.com/5ac3b88da699df4a26f52336eae7c2ce

however when i angle my camera above my character the look vector faces downward. this results in very low dash velocity but when I angle it facing my character horizontally it dashes normally:

https://gyazo.com/3f0b79a1ed218f254f35c73961e1b119

This is the issue:
image

I want the arrow (lookvector) to adjust horizontally so that it doesnt mess up the dash
image

Any idea on how to do this?

if KeysDown.S then
   BodyVelocity.Velocity =cam.CFrame.LookVector * Vector3.new(1,0,1) * -CharacterData.DashVelocity
end

how do i make this script adjust the lookvector horizontally?

You can just multiply LookVector by Vector.new(1, 0, 1). This will remove the vertical aspect to the Cameras LookVector.

2 Likes

thats not the issue, and i did multiply by vector.new(1,0,1) so that the Y value is excluded so the player doesnt fly when they dash, its just that the x and z get really low values when you look above your character and that causes the dash to be really slow. how do i make these values the same as when you angle your camera horizontally so the dash is consistent and normal.

I wouldn’t use the Camera’s LookVector. Use the RightVector (you can achieve a “LeftVector” by multiplying the RightVector by -1) of the HumanoidRootPart for a sideways dash and LookVector for a forwards dash.

2 Likes

I didnt use humanoidrootpart because if your arms are facing the camera then the player would dash forward if it was a sideways dash. and sideways if it was a forward dash. from the cameras perspective.

You can change the orientation of the player to match the Camera before a dash occurs. Make sure Humanoid.AutoRotate is disabled.

2 Likes

before the dash happens, if i can somehow make the arms face left and right and i decide to do an A dash it will go left

Your issue is that the movement isn’t relative to the Cameras direction. You can achieve this by making the character face in the Cameras direction using the Camera LookVector then apply your force.

PrimaryPart.CFrame = CFrame.lookAt(PrimaryPart.CFrame, cameraLookVector * Vector3.new(1,0,1))

force.Force = PrimaryPart.CFrame.LookVector -- Player is now looking at where the camera is, so this will apply a forwards force
2 Likes

image

yeah i honestly have no idea what i am i doing lol

yeah i actually don’t know what I am doing anymore:
https://gyazo.com/a990460a3d446a5eb44c987cadbf2f22

i dont know how to make the player face the camera before the dash. but if thats not the way and theres still a way with camera lookvector i will do it

Once you get the LookVector, you’re going to have to remove the Y axis like @Tom_atoes suggested. But you’ll have to take another step; the x and z axis has to add up to 1(to achieve constant velocity for every dash).

Step 1

Get the look vector, let’s say the look vector was Vector3.new(0.5, 0.2, 0.6).

Step 2

Remove the y axis:

local newVector = Vector3.new(0.5, 0, 0.6)

Step 3

Add up the x and y axis, then divide 1 by that number:

local adjustAmount = 1 / (newVector.X + newVector.Y) -- 0.90909090909

Step 4

You would then multiply the x and y axis by adjustAmount:

local adjustedVector = Vector3.new(newVector.X * adjustAmount, 0, newVector.Z * adjustAmount)
--[[
    Vector = 0.5 * 0.90909090909 + 0.6 * 0.90909090909
           = 0.45454545454 + 0.54545454545
           = 0.99999999999 -- Close enough
--]]

Step 5

Keep on using this calculation to get your dash.


local newVector = camera.CFrame.LookVector -- Vector3.new(0.5, 0, 0.6)
local adjustAmount = 1 / (newVector.X + newVector.Y) -- 0.90909090909
local adjustedVector = Vector3.new(newVector.X * adjustAmount, 0, newVector.Z * adjustAmount)

--[[
    Make the player start dashing in the adjustedVectors direction,
    adjustedVectors direction is moving at 1 stud per unit time, so
    multiply it by x to increase it x times
--]]

BodyVelocity.Velocity = adjustedVector * x

So after trying this.


This if statement runs if no keys are being pressed. it should default to dashing backwards towards the camera

BTW this is running in renderstepped so it follows the camera and deletes the bodyvelocity after its duration is finished.

this is what happened:

https://gyazo.com/2fc176a21741ef351b4b31fe5ad9cfda

im not sure if i did something wrong or this is not right?

That was meant to be per second, not per frame.

So multiply that by the elapsed time:

local T1 = os.clock()
RS.Stepped:Connect(function()
    local elapsedTime = os.clock() - T1
    T1 = os.clock() -- resetting the timer
    BodyVelocity.Velocity = adjustedVector * -CharacterData.DashVelocity * elapsedTime
end)

This will make it move adjustedVector * -CharacterData.DashVelocity studs per second, not per frame.