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

How to accurately get where a Character's head is looking?

Asked by 3 years ago

I need a system for my game where the player is in first person but they can't control their camera and instead it just follows their head. I couldn't find any camera types that let me do that, so I scripted my own system.

My system tweens the camera to the head's position every frame by using RenderStepped. Everything works fine apart from the second CFrame argument, the one where I have to put in where the camera should be looking.

CFrame = CFrame.new(char.Head.CFrame.Position, char.Head.CFrame.lookVector)

The first argument works great and as expected, but the second argument makes the camera constantly look at (0, 0, 0), and when I print out the lookVector of the head, it prints out seemingly random numbers that decrease as I get closer to (0, 0, 0).

I'm not sure if I'm understanding lookVector correctly or if I'm doing it completely wrong, but some guidance would be appreciated.

1
is it on the client or server? 3F1VE 257 — 3y
1
You could use raycasting kepiblop 124 — 3y
0
It's on the client because the camera doesn't exist on the server so it would just error. Spacetimit 47 — 3y
0
I'll look into ray casting as a last resort but I want to see if there is a way to do it without it first. Spacetimit 47 — 3y
View all comments (4 more)
1
It doesn't notify you when I post additional comments to my answer, so I'm putting a comment here so you see thecelestialcube 123 — 3y
0
I did see, it was just really late so I didn't reply. Spacetimit 47 — 3y
0
Just letting you know there's new replies Spacetimit 47 — 3y
0
@thecelestialcube I added a new reply to your answer Spacetimit 47 — 3y

1 answer

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

I think the problem is that you're only using the lookVector for the camera to focus on. To my knowledge, lookVector is a unit vector, which means that all vector3 values x,y,z are between 0 and 1 with a magnitude of 1. Since the CFrame lookAt position is only at the lookVector, it automatically starts at the origin, and your camera ends up looking towards the origin.

To fix this I think you can just add the character's head position to the lookVector.

CFrame = CFrame.new(char.Head.CFrame.Position, char.Head.CFrame.Position + char.Head.CFrame.lookVector)

Not sure if this works but hopefully it helped :)

Update:

--This is a localscript in startgui
local player = game.Players.LocalPlayer
local char = player.Character
local camera = game.Workspace.CurrentCamera

while true do
    if char then
        local head = char:WaitForChild("Head")
        camera.CFrame = CFrame.new(head.CFrame.Position, head.CFrame.Position + head.CFrame.lookVector)
    end

    wait(0.05)
end

I also put a folder in starter character scripts called animate which removes the animations, and this stopped the spinning.

0
I tried this and all it does it make the camera start slowly rotating to the left, then it gets faster and then it turns into an absolute mess. Spacetimit 47 — 3y
1
Ok, I think I found the problem. Roblox character have a natural idling animation to the head, but it's so small that we normally don't notice it. However, it impacts the lookvector enough that it starts slowly rotating. The head turns to the left and the right after a certain amount of time, and that causes the insane mess of the lookvector. thecelestialcube 123 — 3y
0
You can add a folder to startercharacterscripts called animate to remove the animation, but it removes all animations, including walking, jumping, etc. thecelestialcube 123 — 3y
0
I think there are ways you can remove the idling animation by itself, but idk how to do that. thecelestialcube 123 — 3y
View all comments (8 more)
0
Ah, I see. I think a better solution would just be to use the HumanoidRootPart instead and just add a stud or two onto all the Y co-ordinates. I'll see if that works better. Spacetimit 47 — 3y
0
I tested that and it still has the spinning, even though it takes longer to kick in. I tried removing animations completely from the character and running the script but it still violently spins, so I don't think it's the roblox animations. Spacetimit 47 — 3y
0
Really? I made a test version of your script in studio and it worked fine for me without the animations. thecelestialcube 123 — 3y
0
If you want I could post my test script here thecelestialcube 123 — 3y
0
I'd appreciate that Spacetimit 47 — 3y
0
@thecelestialcube Spacetimit 47 — 3y
0
Sorry about not being online for a while, I've updated my answer with my test script. thecelestialcube 123 — 3y
0
Sorry, I've not been on for a while. I figured out that when I run the script while in third person, it runs fine, but if I run it while in first person (my game is first person only) the camera rotates to the left as it did with my script. I also found out that the animations don't affect this at all. Spacetimit 47 — 3y
Ad

Answer this question