2
\$\begingroup\$

I want to have a camera, that follow the player, without childing it to the player object. So far I have this one line, that follows the player, but does not rotate with it (meaning the camera should look where the player's front is looking, how can I do it?

transform.position = player.transform.position + offset;
\$\endgroup\$
  • \$\begingroup\$ Why don't you want to parent the camera to the player? \$\endgroup\$ – Vadim Tatarnikov Jul 30 '15 at 7:40
  • \$\begingroup\$ because if the player dies (gets destroyed) the camera will go with it. I want to make it like some game, where you die, and the camera stays at your corpse for a while, before respawning and the camera gets back to you \$\endgroup\$ – Borislav Jul 30 '15 at 7:41
  • \$\begingroup\$ Just don't destroy the player, play an animation or disable the mesh renderer or collider. \$\endgroup\$ – Vadim Tatarnikov Jul 30 '15 at 7:44
  • \$\begingroup\$ In your die script , you can "deatach" camera from player object \$\endgroup\$ – dnk drone.vs.drones Jul 30 '15 at 7:44
  • \$\begingroup\$ yes, but I need to know how can I make it this way \$\endgroup\$ – Borislav Jul 30 '15 at 7:46
2
\$\begingroup\$

Something like this:

transform.position = player.transform.position - player.transform.forward * distance;
transform.LookAt(player.transform);

where float distance is the distance of camera form player

Consider also the solution 2 : Make camera child of player and in your player die script , you can "deatach" camera from player object

transform.parent = null;
\$\endgroup\$
  • \$\begingroup\$ the code doesn't work, because the camera slowly goes to the player, and then goes inside of him, then out, and its all very weird... \$\endgroup\$ – Borislav Jul 30 '15 at 8:01
  • \$\begingroup\$ still the same, also you can't use multiplications in Vector3 \$\endgroup\$ – Borislav Jul 30 '15 at 8:07
  • \$\begingroup\$ edited , ofset changed in float distance \$\endgroup\$ – dnk drone.vs.drones Jul 30 '15 at 8:19
0
\$\begingroup\$

You can multiply vector3 by using vector3.cross(vector a, vector b);

And here, its not multiplying to vector3. Its multiplying to just z axis. Transform.forward is z axis.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.