- Home /
How to check transform by frame?
Hello, Developers.
I'd like to check difference between now frame and previous frame.
So, I wrote a code like below.
public class Test : MonoBehaviour {
Transform oldPosition;
Transform newPosition;
// Use this for initialization
void Start () {
// Allocate previous Frame's data
oldPosition = GameObject.Find("TestObject");
...
}
// Update is called once per frame
void Update () {
float old_x = oldPosition.rotation.x;
float old_y = oldPosition.rotation.y;
float old_z = oldPosition.rotation.z;
// Allocate now Frame's data
newPosition = GameObject.Find("TestObject");
float new_x = newPosition.rotation.x;
float new_y = newPosition.rotation.y;
float new_z = newPosition.rotation.z;
// Calculate difference between now and previous
float cal_x = new_x - old_x;
float cal_y = new_y - old_y;
float cal_z = new_z - old_z;
...
// **It shows 0, 0, 0!**
Debug.Log ("x : " + cal_x + " y: " + cal_y + " z: " + cal_z);
// Switch now Frame to previous Frame
oldPosition = newPosition;
}
}
I thought that from Start() to Update() takes a few frames. But the result was same. the data allocated in Start() and the data allocated in Update() was same.
The data doesn't same at all. It must not.
What shoud I do for fixing this code?
Answer by instruct9r · Jan 26, 2015 at 10:18 AM
Heya. Not sure if it will hellp you, but it shows the difference, between the last and current frame's rotation:
In your code you are doing it all in one frame. You have to put the cal_x, y, z between the old and new..
Transform myObject;
float old_x;
float old_y;
float old_z;
// Use this for initialization
void Start ()
{
// Allocate previous Frame's data
myObject = GameObject.Find("TestObject").transform;
old_x = myObject.rotation.x;
old_y = myObject.rotation.y;
old_z = myObject.rotation.z;
}
// Update is called once per frame
void Update ()
{
float new_x = myObject.rotation.x;
float new_y = myObject.rotation.y;
float new_z = myObject.rotation.z;
// Calculate difference between now and previous
float cal_x = new_x - old_x;
float cal_y = new_y - old_y;
float cal_z = new_z - old_z;
// **It shows 0, 0, 0!**
Debug.Log ("x : " + cal_x + " y: " + cal_y + " z: " + cal_z);
// Switch now Frame to previous Frame
// oldPosition = newPosition;
old_x = myObject.rotation.x;
old_y = myObject.rotation.y;
old_z = myObject.rotation.z;
}
Answer by GameVortex · Jan 26, 2015 at 10:15 AM
oldPosition and newPosition is both referencing the same Transform object (the "TestObject"). Changing values in the object oldPosition will change the values in newPosition because they are the same Object. You will need to store the rotation values in your own rotation variable. For example in a local Vector3 variable.
A note about Rotation: The rotation variable in Transform is of type Quaternion, a Quaternion does not store angles directly, so accessing and reading the x,y and z values of a Quaternion has no meaning. I recommend accessing the eulerAngles variable of Transform instead if you want to know the difference in angle by degrees.
This is also a very basic object oriented programming issue and I recommend you to go through the Scripting tutorials: http://unity3d.com/learn/tutorials/modules/beginner/scripting
Ah, I forgot EularAngles. Thanks for re$$anonymous$$ding me!