I am trying to do something pretty simple and am having some slight issues.
I have a FirstPersonController and I save the Position and Rotation values on my Awake function. Then on my Reload function I simply apply the saved Position and Rotation values, to ‘Respawn’ my player in their spawn point. The Position value works great! The Rotation however is going all over the place and not preserving the Rotation correctly!
This is what I have, I am using JS/UnityScript:
public var PLAYER_START : GameObject; // I pass the FPSController to this object via the inspector.
private var player_position : Vector3;
private var player_rotation : Quaternion;
function Awake()
{
player_position = PLAYER_START.gameObject.transform.position;
player_rotation = PLAYER_START.gameObject.transform.rotation;
}
.......
function Reload()
{
PLAYER_START.gameObject.transform.position = player_location;
PLAYER_START.gameObject.transform.rotation = player_rotation;
}
Thanks in advance guys! I am sure I am overlooking something silly… lol
Are you looking at the euler angles? A quaternion is shown using euler angles, but there are numerous ways of representing the same rotation using euler angles.
What you could do is caputure the rotation as euler angles instead of quaternions.
private var player_rotation : Vector3;
player_rotation = PLAYER_START.gameObject.transform.eulerAngles;
PLAYER_START.gameObject.transform.eulerAngles= player_rotation;
But the way you are doing it should work.
Also, a minor sidenote which is unrelated:
you don’t need to use PLAYER_START.gameObject.transform
because PLAYER_START is a gameObject.
You can just use PLAYER_START.transform…
I also tried this with eulerAngles and had similar results. Do you think it has something to do with the hierarchy of the FPC? I noticed the camera is actually in the FirstPersonCharacter node. Maybe that has something to do with the odd rotation it is giving me on my Reload method.
The other X factor was, I wasn’t sure if rotation can only be adjusted inside of an Update method, or if I can just set it to preset values with no Lerping or Slerping needed.
Also thanks for the tip! I am not sure why I included gameObject… lol
I dont use the FirstPersonController. But the looking at FirstPersonController.cs, every single variable and function is private, so there’s no way to “Reset” the interal rotation variables.
What you could do is open FirstPersonController.cs and add the following function:
public void ResetMyFPC (Quaternion playerRotation, Quaternion cameraRotation)
{
this.transform.rotation = playerRotation;
this.m_Camera.transform.rotation = cameraRotation;
m_MouseLook.Init(transform , m_Camera.transform); //Reset the target rotation in MouseLook to the current rotations
}
Keep track of both the player and camera initial rotation.
YOu need access to the FPC directly:
function Reload()
{
fpc.ResetMyFPC (player_rotation, camera_rotation);
}
Somehow you have to set the rotation of the character and camera to the values you want, and then reset the m_CharacterTargetRot and m_CameraTargetRot.
You’re going to have to use Debug.Log and Debug.Break() statements to find out what is going on.
Because it isn’t easy to find what causes issues like these.
public var PLAYER_START : GameObject; // I pass the FPSController to this object via the inspector.
private var player_position : Vector3;
private var player_rotation : Quaternion;
function Awake()
{
player_position = PLAYER_START.gameObject.transform.position;
player_rotation = PLAYER_START.gameObject.transform.rotation;
Debug.Log("Captured player rotation: "+player_rotation.eulerAngles);
Debug.Log("Captured camera rotation: "+camera_rotation.eulerAngles);
}
.......
function Reload()
{
PLAYER_START.gameObject.transform.position = player_location;
Debug.Break();
PLAYER_START.gameObject.transform.rotation = player_rotation;
Debug.Log("Restored player rotation: "+PLAYER_START.gameObject.transform.rotation.eulerAngles);
Debug.Log("Restored camera rotation: "+camera_rotation.eulerAngles);
}
Then keep hitting the Next frame button to see what is happeneing. (The next frame button is the button on the right of the play and pause buttons).