This is my camera follow code for a 2D game. The problem I am having is that I am getting the error: error CS1612: Cannot modify a value type return value of `UnityEngine.Transform.position'. Consider storing the value in a temporary variable It has to do with the Clamp camera but I am not sure how to fix this. Any help would be much appreciated. Code (CSharp): public Transform Target; public float moveSpeed = 0.2f; Camera mainCam; void Start () { mainCam = GetComponent<Camera> (); transform.position.x = Mathf.Clamp(transform.position.x,-10, 10); // to prevent the camera from going past a certain point transform.position.y = Mathf.Clamp(transform.position.y,-10, 10); // to prevent the camera from going past a certain point } void Update () { mainCam.orthographicSize = (Screen.height / 100f) / 4f; if (transform) { transform.position = Vector3.Lerp(transform.position, Target.position, moveSpeed) + new Vector3(0, 0, -10); } } }
This is quite a common problem which I see a lot of people face. Basically you should correct it to Code (csharp): Vector3 temp = transform.position: temp.x = Mathf.Clamp(transform.position.x,-10, 10); temp.y = Mathf.Clamp(transform.position.y,-10, 10); transform.position = temp; This error happens because Vector 3 is a struct and transform.position is a getter method
Thanks again the error went away but the camera will still go past the points I specified here is my updated code if you know what I did wrong : Code (CSharp): using UnityEngine; using System.Collections; public class CameraFollow : MonoBehaviour { public Transform Target; public float moveSpeed = 0.2f; Camera mainCam; void Start () { mainCam = GetComponent<Camera> (); Vector3 vect = transform.position; vect.x = Mathf.Clamp(transform.position.x,-2f, 5f); // to prevent the camera from going past a certain point vect.y = Mathf.Clamp(transform.position.y,-9f, 1f); // to prevent the camera from going past a certain point transform.position = vect; } void Update () { mainCam.orthographicSize = (Screen.height / 100f) / 4f; if (transform) { transform.position = Vector3.Lerp(transform.position, Target.position, moveSpeed) + new Vector3(0, 0, -10); } } }
You're clamping the values one time. Start() is called before the first frame an object is rendered on. Instead, move the clamping to the Update() function, probably after transform.position=Vector3 ect.... Update is called every frame, before the object is rendered. So, you want. Code (CSharp): using UnityEngine; using System.Collections; public class CameraFollow : MonoBehaviour { public Transform Target; public float moveSpeed = 0.2f; Camera mainCam; void Start () { mainCam = GetComponent<Camera> (); } void Update () { mainCam.orthographicSize = (Screen.height / 100f) / 4f; if (transform) { transform.position = Vector3.Lerp(transform.position, Target.position, moveSpeed*Time.deltaTime) + new Vector3(0, 0, -10); Vector3 vect = transform.position; vect.x = Mathf.Clamp(transform.position.x,-2f, 5f); // to prevent the camera from going past a certain point vect.y = Mathf.Clamp(transform.position.y,-9f, 1f); // to prevent the camera from going past a certain point transform.position = vect; } } } Also, I added *Time.deltaTime after moveSpeed. This means it will move at a constant rate no matter what your FPS is.