I am novice here. I created a 3D game object (which is a sphere) that moves in circular direction. I have made the object move along x and z axis, with y axis remaining constant as following:
x = Mathf.Cos(angleRad) * radius;
y = 0.5f;
z = Mathf.Sin(angleRad) * radius;
My question is: When I press the ‘Play’ button or when the game object starts to move in a circle,Is there a method in unity that helps to display and store (in a .txt file) the screen co-ordinates of the game object as it moves along in the circle ?
How do I achieve this ?
Thank you
If you want to use the true camera coordinates of the object, you would have to turn these into camera space:
Camera camera; //this needs to be assigned elsewhere
//...
Vector3 relPos = camera.WorldToScreenPoint(transform.position);
float xPos = relPos.x;
float zPos = relPos.y; //not a typo. Camera screen position is x and y, even though your object moves x and z
There’s two good ways that I know of to store this data. The first is PlayerPrefs:
//write
PlayerPrefs.SetFloat("object x", xPos);
PlayerPrefs.SetFloat("object z", zPos);
//read
float x = PlayerPrefs.GetFloat("object x");
float z = PlayerPrefs.GetFloat("object z");
The “camera.WorldToScreenPoint” gives the exact position (x and y position) of the game object (Sphere) in the co-ordinates that correspond to the x and y position of my laptop screen (that runs Unity game engine), right ?