Can't look anywhere while camera following where model is facing?

Hello! So I have a player standing on train script which means that you can stand on the train while it’s moving. Basically this is where I got it: Jailbreak train platform system? - #41 by grilme99
From that topic, I tried the code and it really works. But just a lot of bugs to fix. After I while, I was working together with my friend and until he managed to fix it. The only thing was fixed was that you can now walk to a certain train car. After you walk to the 3rd train car, it will teleport you back to where the previous train car you were standing in.
This was the topic: Player isn't able to move to a certain train car

So after a while, I was working on the train platform code. Until I found a fix to everything. It fixed everything. No more getting pushed back, no more teleporting, no more anything. Everything was all fixed. I can now run to every train car to the very back. Then I run back to the very front with no teleportation and getting pushed back. I was surprised that I managed to fix the entire thing.

So let’s get to the problem! So the problem is now I am trying to add an additional feature to the train platform script. Which is camera following. You know, while you are standing in a train car, it follows where it is facing. Now when I tested it, it sure works but only problem is… I cannot look anywhere. It was like the camera was completely locked to where the train car is facing. What I wanted was while the camera is following where the train car is facing, I want to be also able to look anywhere.

This is the code that makes the camera follow:

local TrainPrimary = workspace.Trains:FindFirstChildWhichIsA("Model").PrimaryPart
local NewCoordinateFrame = TrainPrimary.CFrame
local RelativeCF = NewCoordinateFrame * TrainPrimary.CFrame:Inverse()
Camera.CFrame = RelativeCF  * TrainPrimary.CFrame
local CamRel = Camera.CFrame:ToObjectSpace(Camera.Focus)
Camera.CFrame = Camera.CFrame * CamRel * (RelativeCF - RelativeCF .p) * CamRel:Inverse()

So this is what happens:

As you can see in the footage, you can see me trying to look somewhere but the camera just forces itself to lock where the train car is facing. I have no idea what is causing this camera issue. I call this like camera rotation while standing on an train.

NOTE: That Jailbreak train model, I got it from the toolbox. I was in a rush while typing this topic.

If you have any ideas, suggestions that could fix the problem? Let me know down in the replies! Know what’s causing the problem? Reply below immediately! Greatly appreciated.

Set the Camera type to Fixed or Custom after playing the animation (if you have one).

1 Like

What you could detect where the player wants to move their camera, and add that to the camera cframe. Though I’m not really sure how this would work / how you would pull it off though.

1 Like

@Aerodymier I have tried setting the camera type to custom/fixed before making the camera follow where train car is facing. Just to ensure it don’t just suddenly lock my camera to where the train car is facing.

2 Likes

@Aerodymier They both don’t work. Either you set the camera before or after the camera code, it still doesn’t work. Cannot look anywhere.

Try setting the camera POSITION instead of the camera’s CFrame, since CFrame sets the angle of the camera to a certain angle, and moving the camera with mouse, it also changes the cameras angle, just setting the camera’s position should work!

1 Like

My wording was a little weird, basically delete/comment ur code that sets the CFrame of the camera and retype it to set the Position of the camera to the train.

Reason: When you move your camera, you are basically just changing the orientation of the camera around a camera’s subject/camera’s position. Your code also updates the cameras CFrame which includes the cameras angle, so the only was to fix it is if you make it update the cameras position instead of CFrame.

Thought: If you want to keep it CFrame, you could possibly multiply the cameras CFrame by the current orientation of the camera and that basically cancels your camera’s orientation out of your algorithm and you camera SHOULD move freely once again.

@MichaelProductions01

1 Like

@Instance0new Which line to comment?
This one:

 Camera.CFrame = RelativeCF  * TrainPrimary.CFrame

Or this one:

Camera.CFrame = Camera.CFrame * CamRel * (RelativeCF - RelativeCF.p) * CamRel:Inverse()
1 Like
Camera.Position = (RelativeCF  * TrainPrimary.CFrame).Position
Camera.Position = (Camera.CFrame * CamRel * (RelativeCF - RelativeCF.p) * CamRel:Inverse()).Position

This might actually work, idk about the :Inverse() part but you can possibly do this instead of commenting/deleting anything

1 Like

@Instance0new Alright. I will try this tomorrow in the afternoon. I will let you know what happenes.

1 Like

Okay! Have a good night!

(30 charrssssss)

1 Like

@Instance0new It did not work. It gave me an error message. The error message was:
“Position cannot be assigned to”

Here is the current code:

local TrainPrimary = workspace.Trains:FindFirstChildWhichIsA("Model").PrimaryPart
local NewCoordinateFrame = TrainPrimary.CFrame
local RelativeCF = NewCoordinateFrame * TrainPrimary.CFrame:Inverse()
Camera.CFrame.Position = (RelativeCF  * TrainPrimary.CFrame).Position
local CamRel = Camera.CFrame:ToObjectSpace(Camera.Focus)
Camera.CFrame.Position = (Camera.CFrame * CamRel * (RelativeCF - RelativeCF.p) * CamRel:Inverse()).Position	
1 Like

no no no no, you did it wrong, it should be like this:

local TrainPrimary = workspace.Trains:FindFirstChildWhichIsA("Model").PrimaryPart
local NewCoordinateFrame = TrainPrimary.CFrame
local RelativeCF = NewCoordinateFrame * TrainPrimary.CFrame:Inverse()
Camera.Position = (RelativeCF  * TrainPrimary.CFrame).Position
local CamRel = Camera.CFrame:ToObjectSpace(Camera.Focus)
Camera.Position = (Camera.CFrame * CamRel * (RelativeCF - RelativeCF.p) * CamRel:Inverse()).Position	

Just copy that code ^

(Changed “camera.CFrame.Position” to “camera.Position”)

1 Like

@Instance0new I had to add CFrame because Position is not a valid member of camera. Now what?

So what I did is camera.CFrame.Position.

1 Like
local TrainPrimary = workspace.Trains:FindFirstChildWhichIsA("Model").PrimaryPart
local NewCoordinateFrame = TrainPrimary.CFrame
local RelativeCF = NewCoordinateFrame * TrainPrimary.CFrame:Inverse()
Camera.CFrame = CFrame.new((RelativeCF  * TrainPrimary.CFrame).Position)
local CamRel = Camera.CFrame:ToObjectSpace(Camera.Focus)
Camera.CFrame= CFrame.new((Camera.CFrame * CamRel * (RelativeCF - RelativeCF.p) * CamRel:Inverse()).Position)

try that ^

it SHOULD work this time because i made it where you are creating a new cframe without the orientation

(just copy n paste it)

1 Like

@Instance0new Welp. Tried it. The same thing has happened:

1 Like

Still cannot move my camera freely with rightclick.

1 Like

could you just comment ur code out and use this?

camera.CFrame = train.PrimaryPart.CFrame * CFrame.new(0,5,0) * camera.CFrame

its a long shot but maybe this will work? im running out of ideas lol

1 Like

@Instance0new Now finally! I can look anywhere with right-click! One more bug:
https://streamable.com/phjl06

Now I can look anywhere with right-click. Now is this camera bug. Its like driving the camera crazy. What’s causing that? What a strange glitch/bug.

1 Like

let me have a look at your code.

1 Like