Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

191
Visualizações
Unity camera follows player script

I'm pretty new to Unity. I tried to create a script that the camera would follow the actor (with a little difference). Is there a way to improve the code? It works just fine. But I wonder if I did it the best way. I want to do it about as I wrote, so if you have any tips. Thank you

Maybe change Update to FixedUpdate ?

public GameObject player;

// Start is called before the first frame update
void Start()
{
    player = GameObject.Find("Cube"); // The player
}

// Update is called once per frame
void Update()
{
    transform.position = new Vector3(player.transform.position.x, player.transform.position.y + 5, player.transform.position.z - 10);
}
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Making the camera following the player is quite straight forward.

Add this script to your main camera. Drag the reference of the player object to the script and then you are done.

You can change the values in the Vector 3 depending on how far you want the camera to be from the player.

using UnityEngine;

public class Follow_player : MonoBehaviour {

    public Transform player;

    // Update is called once per frame
    void Update () {
        transform.position = player.transform.position + new Vector3(0, 1, -5);
    }
}
over 4 years ago · Santiago Trujillo Relatório

0

This will always follow the player from the same direction, and if the player rotates it will still stay the same. This may be good for top-down or side-scrolling view, but the camera setup seems to be more fitting for 3rd person, in which case you'd want to rotate the camera when the player turns.

The easiest way to do this is actually not with code alone, simply make the camera a child of the player object, that way its position relative to the player will always stay the same!

If you do want to do it through code, you can change the code to be like this:

void Update()
{
    Vector3 back = -player.transform.forward;
    back.y = 0.5f; // this determines how high. Increase for higher view angle.
    transform.position = player.transform.position - back * distance;

    transform.forward = player.transform.position - transform.position;
}

You get the direction of the back of the player (opposite of transform's forward). Then you increase the height a little so the angle will be a bit from above like in your example. Last you set the camera's position to be the player's position and add the back direction multiplied by the distance. That will place the camera behind the player.

You also need to rotate the camera so it points at the player, and that's the last line - setting the camera's forward direction to point at the player.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda