Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a camera follow an object in unity3d C#?

Tags:

I have an object called Ball, and I added keyboard interactivity to it(WASD to move the ball) I need the camera to stay behind and follow the ball, but I am getting errors.

using UnityEngine;
using System.Collections;
public class ballmain : MonoBehaviour {
    public bool isMoving = false;
    public string direction;
    public float camX;
    public float camY;
    public float camZ;
    // Use this for initialization
    void Start () {
        Debug.Log("Can this run!!!");
    }

    // Update is called once per frame
    void Update () {
        camX = rigidbody.transform.position.x -=10;
        camY = rigidbody.transform.position.y -=10;
        camZ = rigidbody.transform.position.z;
        camera.transform.position = new Vector3(camX, camY, camZ);
            //followed by code that makes ball move
    }
}

I get error "Assets/ballmain.cs(18,44): error CS1612: Cannot modify a value type return value of 'UnityEngine.Transform.position'. Consider storing the value in a temporary variable" Does anyone know the answer? If I comment out the code about the camera the ball can move around.