Updating Current rotation data on firstpersonlook from load game file
Hi guys,
I have a question i am making a save and load system it almost works when i save my position and rotation the data is saved and when i load it is also loaded correctly.
But when i load a save my rotation works for 1 sec and snaps back at the original position the problem is i cant or don't know how to update my character.localRotation
in my FirstPersonLook
script with the y and x data. i tried this
this.GetComponentInChildren<FirstPersonLook>().character.transform.eulerAngles = rotation;
my data is stored like this
Vector2 rotation;
rotation.x = data.rotation[0];
rotation.y = data.rotation[1];
this is the firstpersonlook script
// Rotate camera and controller.
transform.localRotation = Quaternion.AngleAxis(-currentMouseLook.y, Vector3.right);
character.localRotation = Quaternion.AngleAxis(currentMouseLook.x, Vector3.up);
here are the scripts.
SaveLoad.cs
using System.Collections;
using UnityEngine;
public class SaveLoad : Photon.MonoBehaviour
{
public int level = 3;
public int health = 40;
public int inventory_1;
public bool isinvertory = false;
public GameObject Player;
void Update ()
{
if(isinvertory == true)
{
isinvertory = true; // Example
}
if(inventory_1 == 1)
{
isinvertory = true; // Example
}
if (inventory_1 == 0)
{
isinvertory = false; // Example
}
}
public void SavePlayer ()
{
Player = GameObject.FindGameObjectWithTag("Player");
SaveSystem.SavePlayer(this);
}
public void LoadPlayer ()
{
Player = GameObject.FindGameObjectWithTag("Player");
PlayerData data = SaveSystem.LoadPlayer();
level = data.level;
health = data.health;
isinvertory = data.isinvertory;// Example
inventory_1 = data.inventory_1;// Example
Vector3 position;
position.x = data.position[0];
position.y = data.position[1];
position.z = data.position[2];
Vector2 rotation;
rotation.x = data.rotation[0];
rotation.y = data.rotation[1];
if (GetComponent<FirstPersonMovement>() == null)
{
// Do Nothing..
}
else
{
this.GetComponent<FirstPersonMovement>().enabled = false;
Player.GetComponentInChildren<FirstPersonLook>().enabled = false;
StartCoroutine(FirstPersonMovementEnableCoroutine());
}
if (GetComponent<PhotonView>() == null)
{
// Do Nothing..
}
else
{
this.GetComponent<FirstPersonMovement>().enabled = false;
Player.GetComponent<PhotonView>().transform.position = position;
Player.GetComponent<PhotonView>().transform.eulerAngles = rotation;
Player.GetComponentInChildren<FirstPersonMovement>().transform.eulerAngles = rotation;
this.GetComponentInChildren<FirstPersonLook>().character.transform.eulerAngles = rotation;
}
Player.transform.position = position;
Player.transform.eulerAngles = rotation;
Player.GetComponentInChildren<FirstPersonMovement>().transform.eulerAngles = rotation;
this.GetComponentInChildren<FirstPersonLook>().character.transform.eulerAngles = rotation;
//this.GetComponent<FirstPersonMovement>().enabled = true;
if (level == 1)
{
BroadcastMessage("LoadLevel_The_Station");
}
}
IEnumerator FirstPersonMovementEnableCoroutine()
{
//yield on a new YieldInstruction that waits for 5 seconds.
yield return new WaitForSeconds(0.1f);
this.GetComponent<FirstPersonMovement>().enabled = true;
this.GetComponentInChildren<FirstPersonLook>().enabled = true;
}
}
First personlook..
FirstPersonLook.cs
using UnityEngine;
public class FirstPersonLook : MonoBehaviour
{
[SerializeField]
public Transform character;
Vector2 currentMouseLook;
Vector2 appliedMouseDelta;
public float sensitivity = 1;
public float smoothing = 2;
public Animator anim;
public float currentWeight = 0f;
public float speedTransission = 1f;
void Reset ()
{
character = GetComponentInParent<FirstPersonMovement>().transform;
}
void Start ()
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
void Update ()
{
if(Input.GetAxisRaw("Mouse X") == 0)
{
//currentWeight = Mathf.Lerp(currentWeight, 0.0f, Time.deltaTime); anim.SetLayerWeight(1, currentWeight);
//anim.SetLayerWeight(anim.GetLayerIndex("UpperBody"), currentWeight = 0f);
}
else
{
//currentWeight = Mathf.Lerp(currentWeight, 1.0f, Time.deltaTime); anim.SetLayerWeight(1, currentWeight);
// anim.SetLayerWeight(anim.GetLayerIndex("UpperBody"), currentWeight = 1f);
}
// Get smooth mouse look.
Vector2 smoothMouseDelta = Vector2.Scale(new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")), Vector2.one * sensitivity * smoothing);
appliedMouseDelta = Vector2.Lerp(appliedMouseDelta, smoothMouseDelta, 1 / smoothing);
currentMouseLook += appliedMouseDelta;
currentMouseLook.y = Mathf.Clamp(currentMouseLook.y, -48, 90);
// Rotate camera and controller.
transform.localRotation = Quaternion.AngleAxis(-currentMouseLook.y, Vector3.right);
character.localRotation = Quaternion.AngleAxis(currentMouseLook.x, Vector3.up);
}
}
What you need to save/load in this case is firstPersonLook.currentMouseLook
and everything will be fine
Your answer
Follow this Question
Related Questions
Rotating an Object (To Face Another Object) Only on X and Y Axis 3 Answers
How to make a 2D sprite rotate towards a specific point while facing perpendicular to the camera ? 1 Answer
LookRotation with constant speed 0 Answers
Rotation issues - Please help 0 Answers
Optimizations: Should I use Update or FixedUpdate? Could you help me optimize rotations? 1 Answer