Be the first to post for 'A Simple DIY Follow Camera'!

If you’re reading this, there probably aren’t very many posts yet. But don’t worry, you can be the first! Either create a new post or just reply to this one to say ‘hi’.

Hey, i am a recent college graduate (Computer Information Systems – Basically Software Engineering/Data Mining), and i am enjoying using Unity3D along with you. I have used Unity before as well as CryEngine, so i am somewhat experienced.

I already have my concept art i drew myself and i will be using Blender to bring my creations to life!

At the challenge A Simple DIY Follow Camera I resolve it in different way to the video:

using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour {

public GameObject player;

// Update is called once per frame
void LateUpdate () {
this.transform.position = player.transform.position;
}
}

Instructor…What is better or faster? Search the GameObject by tag or assign the player to the script? Or Is it indifferent?

Hi Karina,

From what i have read, it’s not very good for perfomance to always use the search game object with tags. If you have a scene or a project with a lot of objects, the search will go through all of them to find the one that you need.
Because of that, i did the code a little different from yours:

public class CameraFollow : MonoBehaviour
{
    public GameObject playerPosition;
 
    // Use this for initialization
    void Start()
    {
        if (playerPosition == null)
        {
            playerPosition = GameObject.FindGameObjectWithTag("Player");
        }
    }

    private void LateUpdate()
    {
        transform.position = playerPosition.transform.position;
    }
}

I don’t know if that is the best solution, but if i ever forget to place the player in the inspector, then i will try to find it.
:slight_smile:

ps: sorry for any grammar mistake, english is not my first language.

Hi there, as we only need to find the player at the Start() it doesn’t really matter which way we do it. With performance in general, we focus on our code being clean and maintainable, then worry about speed when our profiler tells us to.

Privacy & Terms