- Home /
Transform.position returns local position
Hello there, I am IT student and I'm messing around Unity, making TPS style game. From time to time, my CameraMovementScript seems to crash and transform.position returning local position of following pivot, as a result - camera is following global (0,0,0) point instead of player.
To fix this I only need to restart Unity. Then it happens again in few times "Play" clicked.
It is highly unlikely to be a Unity problem and very likely to be an issue with your script. We need to see your script for any kind of specific answer.
@robertbu Cannot be my script issue, once I click "play" camera follows global 0,0,0. Then i restart unity without ANY changes, click play again and camera follows player
That doesn't conclusively rule out a script issue; as an example, you could have a race condition during the first frame. Are you seeing any error messages in the console window?
Doesn't sound like an issue where position == localPosition.
LocalPosition is only a thing if you are exa$$anonymous$$ing a child object. It's the position relative to the parent. If an object has no parent, localPos is the same as (global) position.
Is your "follow pivot" an empty child behind the player? Even so, try printing out the pivot.position
to check. If it suddenly starts printing (0,2,-10) over and over, no matter how the player spins and moves, then yes, position is glitching and returning localPosition.
I don't clearly understand the issue, but another thing I observed is that while running, dirt smoke should be appear at (0,0,0) local foot position which changes during run. When problem occurs, all footstep smokes are visible at global (0,0,0)
Answer by Slayer1928 · Feb 11, 2014 at 10:33 PM
Try another camera follow script?
public Transform target;
public float distance = 10.0f;
public float height = 3.0f;
public float heightDamping = 2.0f;
public float rotationDamping = 3.0f;
void LateUpdate () {
if (!target)
return;
Debug.Log(distance.ToString());
float wantedRotationAngle = target.eulerAngles.y;
float wantedHeight = target.position.y + height;
float currentRotationAngle = transform.eulerAngles.y;
float currentHeight = transform.position.y;
currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
Quaternion currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
transform.position = target.position;
transform.position -= currentRotation * Vector3.forward * distance;
Vector3 temp = transform.position;
temp.y = currentHeight;
transform.position = temp;
transform.LookAt (target);
}
I am using exatly the same script as yours, with different variable names
Answer by qwinzeth · May 15, 2019 at 03:51 AM
I basically used GameObject.Find("Parent").transform.Find("Child").position to work around what I believe to be a similar issue.
My similar, strange problem is that a prefab ("Boss") contains a child ("CameraAnchor") and GameObject.Find("CameraAnchor") would sometimes, every now and then, return (0,4.5,0) as transform.position, which is actually the localPosition. It seemed to happen sometimes when restarting the scene, but not reliably. Usually transform.position would return the expected global position of (230, 4.5, 0)
I was able to work around this strange problem by grabbing GameObject.Find("Boss"), and then using transform.Find("CameraAnchor")
Here is my mess of a function I eventually cobbled together. Most code probably can't use "x > 1" as a "valid" position, but it works for me because a Boss is never at the beginning of a level :)
public void LockCamera(Vector2 lockPosition)
{
// This is normal, everything is fine
if (lockPosition.x >= 1)
{
_isPositionLocked = true;
_lockPosition = lockPosition;
return;
}
// Uh oh, Unity done goofed and gave us the localPosition for some reason...
Debug.Log("INVALID CameraScript.LockCamera(lockPosition = " + lockPosition);
GameObject bossObject = GameObject.Find("Boss");
// Serious problem here
if (bossObject == null)
{
Debug.Log("Unable to Find Boss object, aborting LockCamera");
return;
}
Debug.Log("Boss at " + bossObject.transform.position);
_lockPosition = bossObject.transform.position;
if (_lockPosition.x >= 1)
{
_isPositionLocked = true;
}
// We can try to get the CameraAnchor ourselves
Transform cameraAnchorObjectTransform = bossObject.transform.Find("CameraAnchor");
if (cameraAnchorObjectTransform == null)
{
// Well, we tried.
Debug.Log("Unable to Find CameraAnchor, using Boss if able");
return;
}
else if (cameraAnchorObjectTransform.position.x >= 1)
{
Debug.Log("Found CameraAnchor at " + cameraAnchorObjectTransform.position);
_lockPosition = cameraAnchorObjectTransform.position;
_isPositionLocked = true;
return;
}
Debug.Log("Found CameraAnchor at INVALID, using Boss if able " + cameraAnchorObjectTransform.position);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613133218im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Mouse orbit and problems with raycast 0 Answers
Renderer on object disabled after level reload 1 Answer
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer