How to move camera thats attached to player


How to move camera thats attached to player



I'm trying to recreate a dolphin dive type of feel, as of right now my character is a cube with no animation so I feel like the best way to do this would be to manually move the camera as if the character had just dolphin dived but every time I try to move the camera the entire player itself moves.



Code:


if (Input.GetKeyDown(KeyCode.C) && isSprinting) {
for (float i = 0; i <= 10; i++) {
var forceForward = 8f;
if (isGrounded == true && moveVertical != 0) {
_rgb.AddForce(fpsCam.transform.forward * forceForward);
for (float j = 0; j <= 10; j++) {
_camera.AddForce(Vector3.up * 3f);
}
}
}
}



if you need any more information let me know!





Sorry, my crystal ball is dirty. _rgb, _camera, hierarchy? ...
– Quasimodo's clone
Jun 30 at 4:45



_rgb


_camera





And what does "camera attached" mean?
– Quasimodo's clone
Jun 30 at 4:55






I only wrote that much code to avoid writing out a bunch of code that's not necessary to my question I avoided it, _rgb is the rigidbody for my player, and I also added a rigidbody to the camera, turned gravity off, and made it kinematic hoping I could move the rigid body of the camera and not the player but it didn't work. so _rgb is the rigid body to the player, and _camera is the rigid body for the camera. And when I say camera attached, keep in mind I'm new to unity, so theres probably a different word for it but It just means I've attached the camera to the player object
– Trevn Jones
Jun 30 at 19:05





1 Answer
1



If you want a shortcut, you can make the camera gameobject a child of the player game object. This way it will move with the player.





I end up doing this surprisingly often. Possible pitfalls include accidentally deleting the player object between scenes, especially to non-game scenes like a main menu, and forgetting to move the camera by relative rather than absolute coordinates afterwards
– David
Jun 30 at 2:16






"How to move camera thats attatched to player" - move the player...
– Rodrigo Rodrigues
Jun 30 at 3:50





There are situations where this isn't the best solution though so I can see why he would ask this question. For example, if you wanted the camera to lag slightly behind the player. In that case, you would require a more complex solution.
– pseudoabdul
Jun 30 at 3:59





@RodrigoRodrigues "move the player..." The Q tries to move the cam in some way without moving also the player.
– Quasimodo's clone
Jun 30 at 4:52







By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Render GeoTiff to on browser with leaflet

How to get chrome logged in user's email id through website

using states in a react-navigation without redux