- Home /
Camera follow player with code
I need my camera to follow my player however I want to do it through code as I don't want the camera to have the player's rotation. This is why I don't want it to be a child. I would like my camera to be like this :
https://gyazo.com/53eca30931a7eee7a901e15aafa62607
but move with my player.
How would I do this through code?
Answer by AhsanNaeem · Apr 28, 2017 at 01:35 PM
Here is the detailed video for your query: [https://www.youtube.com/watch?v=Xcm5H2J95iI∈dex=4&list=PLX2vGYjWbI0Q-s4_lX0h4i2zbZqlg4OfF]
Answer by AtGfx · Apr 28, 2017 at 07:25 AM
One solution can be to reference the camera attached to the player in the player movement script. Then when you detect that your player move, you move your camera according to the displacement. This way your camera is not a child of your player and you can move it as you want.
I think that this way of moving the camera is fully described in the Unity tutorial here.
Answer by unity_UfYiquMAgkCKbQ · Mar 04, 2019 at 01:33 PM
I presume @AhsanNaeem is Unity Staff, so I hate to contradict him.
But as of Unity 2018.3.6f, that video doesn't work.
however, I found this video (https://www.youtube.com/watch?v=urNrY7FgMao) which is so much better. Bonus, he adds cameral lag to the follow camera, and shows how to do so.
Your answer
Follow this Question
Related Questions
Multiple Targets Camera 1 Answer
Keep camera away from head while using Lerp 0 Answers
Moving Player Relative to Camera? 1 Answer
Camera problem 2 Answers
How can I get a Capsule to move and rotate in the direction the cam is facing? 1 Answer