I don’t understand why transform.position is returning the same value before and after I change it’s value in this script. I’ve obviously changed the camera to a new position, yet the console shows the same vector printed before and after the change? My script works perfectly and the camera does move. Can anyone explain why the console doesn’t show this? It’s really messing with my head when it comes to debugging and figuring out the math of moving cameras.
using UnityEngine;
using System.Collections;
public class cameraFollow : MonoBehaviour {
public Transform target;
public Vector3 offset;
void Start () {
offset = transform.position;
}
void LateUpdate () {
print ("--------------------------------------------------------------");
print (transform.position);
transform.position = target.transform.position + offset; // move camera to desired position
print (transform.position);
print ("--------------------------------------------------------------");
}
}
Is offset a small value? Logging tends to round values.
2 Likes
No it’s fairly large. I even changed the script to make it even more obvious what’s going on and tried using debug.log instead. The camera is always an exact offset from the player now. I can see the camera pop to it’s new position on the terrain so it’s just above the character. I can move the camera anywhere and it will follow as the player moves. Yet the before and after of transform.position is coming out the same. So weird
using UnityEngine;
using System.Collections;
public class cameraFollow : MonoBehaviour {
public Transform target;
Vector3 offset;
void Start () {
Debug.Log ("character current at transform.position" + target.transform.position);
// always offset camera by this amount from character position
offset.x = -30f;
offset.y = 42f;
offset.z = -30f;
// character current at transform.position 50,0,0
}
void LateUpdate () {
print ("--------------------------------------------------------------");
Debug.Log (transform.position);
transform.position = target.transform.position + offset; // move camera to desired position
Debug.Log (transform.position);
print ("--------------------------------------------------------------");
}
}
Oh but wait… it is a tiny amount if it’s in Void Update because it’s only moving by a small amount when the movement key is pressed. Good point. It’s probably rounded. I have to figure out how to turn that off
Yes that was it! It works now. Thanks I was going crazy. I didn’t realize the log rounded. I used this code instead and I can see the small changes:
Debug.Log (transform.position.x);
Debug.Log (transform.position.y);
Debug.Log (transform.position.z);
transform.position = target.transform.position + offset; // move camera to desired position
Debug.Log (transform.position.x);
Debug.Log (transform.position.y);
Debug.Log (transform.position.z);
For future reference, I’m pretty certain that Vector3.ToString() accepts ordinary .NET format strings, so the following code will show you a lot more precision:
Debug.Log(transform.position.ToString("N6"));