Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!