1
\$\begingroup\$

I've recently started to learn some C# scripting in Unity. I've been able to make my PlayerCube move around using Vector3 Transform.translations, but I want my camera to follow my cube while moving around, and it's not working since Unity can't find PlayerCube. How do I make make it so it understands that there's a PlayerCube?

using UnityEngine;
using System.Collections;

public class CameraScript : MonoBehaviour {
    Player = GameObject.Find("PlayerCube");

    // Use this for initialization
    void Start () 
    {
    //  GameObject.Find(PlayerCube);
    }

    // Update is called once per frame
    void Update () 
    {
        Transform.LookAt (PlayerCube);
    }
}
\$\endgroup\$
1
  • \$\begingroup\$ What is the error you're getting? Is it at compile time or at build time? \$\endgroup\$ – Vaillancourt Mar 21 '15 at 21:50
1
\$\begingroup\$

the fastest way is to make the camera child of the cube (in the editor), then in the Update() of the camera script use LookAt :

void Update() {
    transform.LookAt(transform.parent);
}
\$\endgroup\$
1
  • \$\begingroup\$ Brownie points for simplicity. Doesn't get much simpler than this. \$\endgroup\$ – Tom 'Blue' Piddock May 21 '15 at 13:09
1
\$\begingroup\$

You can use this script. Attach it to your camera, and place your cube in the Target spot in the inspector.

using UnityEngine;
using System.Collections;

public class TargetFollower : MonoBehaviour
{

    [Tooltip("Target to be followed")]
    public Transform Target;
    [Tooltip("Mimic the Target's changes in x coordinate")]
    public bool FollowTargetX = true;
    [Tooltip("Mimic the Target's changes in y coordinate")]
    public bool FollowTargetY = true;
    [Tooltip("Mimic the Target's changes in z coordinate")]
    public bool FollowTargetZ = true;

    // Update is called once per frame
    void FixedUpdate()
    {
        if (Target)
        {
            Vector3 targetMovement = GetTargetMovement();
            UpdatePosition(targetMovement);
        }
    }

    private Vector3 oldTargetPosition;
    private Vector3 GetTargetMovement()
    {
        if (oldTargetPosition == Vector3.zero)
        {
            oldTargetPosition = Target.transform.position;
        }
        Vector3 newTargetPosition = Target.transform.position;
        Vector3 targetMovement = newTargetPosition - oldTargetPosition;
        oldTargetPosition = new Vector3(newTargetPosition.x, newTargetPosition.y, newTargetPosition.z);
        return targetMovement;
    }

    private void UpdatePosition(Vector3 targetMovement)
    {
        float xPosition = transform.position.x;
        float yPosition = transform.position.y;
        float zPosition = transform.position.z;
        if (FollowTargetX)
        {
            xPosition += targetMovement.x;
        }
        if (FollowTargetY)
        {
            yPosition += targetMovement.y;
        }
        if (FollowTargetZ)
        {
            zPosition += targetMovement.z;
        }
        Vector3 updatedPosition = new Vector3(xPosition, yPosition, zPosition);
        transform.position = updatedPosition;
    }
}
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Brownie points for providing axis selection. \$\endgroup\$ – Tom 'Blue' Piddock May 21 '15 at 13:08
0
\$\begingroup\$

By looking at your script and if it's the exact same in Unity you have a problem with the variable Player (no type and wrong name in Update ()). And you need to pass a Transform and not a GameObject in tranform.LookAt ().

using UnityEngine;
using System.Collections;

public class CameraScript : MonoBehaviour {
    GameObject Player = GameObject.Find("PlayerCube");

    void Update () 
    {
        transform.LookAt (Player.tranform);
    }
}

I don't recommand making the camera a child of the cube as suggest @dnk-drone.vs.drones.

If you only want your camera to look at your object then the transform.LookAt () is what you need. If you want to move the camera with the object you need to write it in your script.

You can also use a public reference for your object and drag it by using the Unity inspector.

Also, you can watch in the Unity/Standard Assets, a CameraFollow script exist already.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.