Search code examples
c#cameraunity-game-engineunityscript

Camera Smooth Follow 2D from UnityScript to C# using Unity3D


I am making a script to be used on the Main Camera object of Unity3D so the camera follows the character in a 2D platformer world.

I tried to translate this from a UnityScript script to c#, I am getting an error in line 26: "Cannot modify a value type return value of 'UnityEngine.Transform.position'. Consider storing the value in a temporary variable."

Original UnityScript Version

var cameraTarget : GameObject;
var player : GameObject;

var smoothTime : float = 0,1;
var cameraFollowX : boolean = true;
var cameraFollowY : boolean = true;
var cameraFollowHeight : boolean = false;
var cameraHeight : float = 2.5;
var velocity : Vector2;
private var thisTransform : Transform;

function Start ()
{
  thisTransform = transform;
}

function Update () 
{

if (cameraFollowX)
{
  thisTransform.position.x = Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x, velocity.x, smoothTime);
}

if (cameraFollowY)
{
  thisTransform.position.y = Mathf.SmoothDamp (thisTransform.position.y, cameraTarget.transform.position.y, velocity.y, smoothTime);
}

if (!cameraFollowX & cameraFollowHeight)
{
  camera.transform.position.y = cameraHeight;
}

}

My C# Version

using UnityEngine;
using System.Collections;

public class CameraSmoothFollow : MonoBehaviour {

    public GameObject cameraTarget; // object to look at or follow
    public GameObject player; // player object for moving

    public float smoothTime = 0.1f; // time for dampen
    public bool cameraFollowX = true; // camera follows on horizontal
    public bool cameraFollowY = true; // camera follows on vertical
    public bool cameraFollowHeight = true; // camera follow CameraTarget object height
    public float cameraHeight = 2.5f; // height of camera adjustable
    public Vector2 velocity; // speed of camera movement

    private Transform thisTransform; // camera Transform

    // Use this for initialization
    void Start () {
        thisTransform = transform;
    }

    // Update is called once per frame
    void Update () {
        if (cameraFollowX){
            thisTransform.position.x = Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x, ref velocity.x, smoothTime); // Here i get the error
        }
        if (cameraFollowY) {
        // to do    
        }
        if (!cameraFollowX & cameraFollowHeight) {
        // to do
        }
    }
}

Any help will be appreciated.


Solution

  • This error occurs because Transform.position is of a value type (probably a struct). This means that when you access the X property of position, you are accessing a COPY and NOT the real thing. To assign to the position property, you will need to create a new Vector3 and assign it to the position property. Your code will look something like this:

    thisTransform.position = new Vector3 (Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x, ref velocity.x, smoothTime), thisTransform.position.y, thisTransform.position.z);
    

    or perhaps more cleaner:

    float tempX = new Vector3 (Mathf.SmoothDamp (thisTransform.position.x, cameraTarget.transform.position.x, ref velocity.x, smoothTime);
    thisTransform.position = new Vector3 (tempX, thisTransform.position.y, thisTransform.position.z);