Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Wesley21spelde · Jan 12 at 02:25 AM · rotationupdatelookrotationcurrentfirstperson

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);
     }
 }








Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image andrew-lukasik · Jan 12 at 07:52 PM 0
Share

What you need to save/load in this case is firstPersonLook.currentMouseLook and everything will be fine

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

223 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges