A simple DIY follow camera

New to Unity but not to programming. I took a long approach, declaring the player as a public game object and dragging it to the script attributes in the inspector. Obviously not the most elegant approach. :flushed: Glad to see it is a lot easier than that, but i suppose it might work if we had multiple player instances and want to dynamically change the camera to a different player instance.

what do you mean “multiple player instances” in this case? You can swap which character / player object to control in your game scene?

Sure. I remember an old-school 2d top-down game on nintendo. You control your initial character around and solve some puzzles. Eventually you talk to an NPC and he has some other skills. He offers to do a quest for you. After you accept his offer, you were now controlling the “NPC” and your initial character sat there until you went back and spoke to him.

Now imagine you wanted a similar feature. As far as the CameraFollow.cs script is concerned, it just follows a GameObject. So in the script you when you declare the player variable, you could declare it public. Then instead looking up the player using player = GameObject.FindGameObjectWithTag(“Player”)
you could remove the line and instead drag the Player object from the hierarchy into the public player slot under the CameraArms Camera Follow component.

Then, later in your game dev when you make the NPC and write the interactivity. you could dynamically change the player variable to the new player object.

In fact, it might be a good idea rename the player variable to ‘subject’. This way, if you had a scene where you had an object, lets say a horse running, you could use the same script and just change the public ‘subject’ GameObject to the horse object.

hope that makes sense

I see were you are coming from. Just thinking of another way were you have your different tags you want to have select-able in an array of strings then check if current subject is controllable, were to change the object in the first place… your method might be better

string [] myControllableSubjects = {"Player", "NPC", "Companion"}

// loop through array here??
{
GameObject.FindGameObjectsWithTag (myControllableSubjects);
}

just thinking out loud here :slight_smile:

Privacy & Terms