0
\$\begingroup\$

How to do camera following an object with special tag without drag and drop into Inspector? I have 2 prefab model they are not into hierarchy for the moment when player start play. To load selected model into scene I'm using the below code.

public GameObject[] players;
void Start () 
{
LoadPlayer ();
}
private void LoadPlayer()
{       
GameObject player =Instantiate(players[PlayerPrefs.GetInt(MyModel")]);
}

Because in other scene I can change my player model but I can't follow it using in c# script for camera public Transform cause of I can't drop it in that place in Inspector. So in what way I can find current player object when game start to follow it by camera?

\$\endgroup\$
1
\$\begingroup\$

give this script to player.this script find camera then give camera.position so you can use Lerp for following

using UnityEngine;
using System.Collections;

public class CameraFollow : MonoBehaviour {


    void Update () {
        Vector3 offest = new Vector3 (3,3,3);//Distance from object
        Vector3 cameraPos = Camera.main.transform.position;//position of camera
        Camera.main.transform.position= Vector3.Lerp (new Vector3(cameraPos.x,cameraPos.y,cameraPos.z), transform.position + offest, 0.8f *Time.deltaTime));
    }
}

If you want follow player just In one axis you can replace zero In upper code for example if you want follow player In vector3.x you can do It easily:

Camera.main.transform.position= Vector3.Lerp (new Vector3(cameraPos.x,0,0), transform.position + offest, 0.1f);
\$\endgroup\$
  • \$\begingroup\$ But I have one more problem with that code, when I use that script my player object shakes when I move it. Is there any way to cut down that shakes? \$\endgroup\$ – MMon Jul 3 '16 at 21:23
  • \$\begingroup\$ if shakes mean camera smooth moving , you can remove Time.deltaTime from that code or you can don't use Lerp same this: Camera.main.transform.position = transform.position + offest; \$\endgroup\$ – Seyed Morteza Kamali Jul 3 '16 at 21:35
1
\$\begingroup\$

You want to use GameObject.FindWithTag which will return the first active GameObject in the scene that matches the tag you pass in as the parameter. So assuming your player GameObject uses the tag Player, you would do the following.

void Start() {
    Transform player = GameObject.FindWithTag("Player").transform;
}
\$\endgroup\$
  • 1
    \$\begingroup\$ But to follow it by camera I mus use Transform from that object? Yes? \$\endgroup\$ – MMon Jul 2 '16 at 21:53
0
\$\begingroup\$

In the player script, you could find the camera and make it follow the player with:

GameObject cam = GameObject.FindWithTag("Camera"); //assuming Camera has tag Camera
cam.transform.setParent(this.gameObject);

This makes the player the parent of the camera. This essentially does what the drag and drop does, but through code, so it works with prefabs.

\$\endgroup\$
  • \$\begingroup\$ If I add that code to player script it call me UnityEngine.GameObject' does not contain a definition for setParent' and no extension method setParent' of type `UnityEngine.GameObject' could be found \$\endgroup\$ – MMon Jul 3 '16 at 18:07
  • \$\begingroup\$ In child and parent you should use transform for example : Camera.main.transform.parent = transform; so you can fix your problem >> Camera.main.transform.SetParent(gameObject.transform,true); \$\endgroup\$ – Seyed Morteza Kamali Jul 3 '16 at 20:52
  • \$\begingroup\$ @MichałMasłoń oops, sorry. I didn't have unity open, so i assumed setParent was a method of the gameobject. I corrected my answer. \$\endgroup\$ – coolioasjulio Jul 4 '16 at 4:27

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.