I am making a top down game. I have 2 scenes, each of them has trigger elements that change the scene. There is a ScriptableObject “Player Position”, which sets the coordinate to which the player will move when changing the scene. The problem is that when I change the scene and stop the game on it, and then press “play” again, the player is transferred to the position where he was on the previous scene. I was thinking of solving this by adding an ISerializationCallbackReceiver to my script. I designate the standard position of the player on the stage and the one to which I need to move, and when I move to another stage and press “play” on it, the idea is that the position of the player should be equated to the standard one. But this is not happening. Why is this happening? I am using unity 21.3.13f1
immenseklutzybanteng
Player Movement script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
private Rigidbody2D myRigidbody;
private Vector3 change;
private Animator animator;
public VectorValue startingPosition;
// Start is called before the first frame update
void Start()
{
animator = GetComponent<Animator>();
myRigidbody = GetComponent<Rigidbody2D>();
transform.position = startingPosition.initialValue;
}
// Update is called once per frame
void FixedUpdate()
{
change = Vector3.zero;
change.x = Input.GetAxisRaw("Horizontal");
change.y = Input.GetAxisRaw("Vertical");
UpdateAnimationAndMove();
}
void UpdateAnimationAndMove()
{
if (change != Vector3.zero)
{
MoveCharacter();
animator.SetFloat("moveX", change.x);
animator.SetFloat("moveY", change.y);
animator.SetBool("moving", true);
}
else
{
animator.SetBool("moving", false);
}
}
void MoveCharacter()
{
myRigidbody.MovePosition(
transform.position + change * speed * Time.fixedDeltaTime
);
}
}
VectorValue script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[CreateAssetMenu]
public class VectorValue : ScriptableObject, ISerializationCallbackReceiver
{
public Vector2 initialValue;
public Vector2 defaultValue;
public void OnAfterDeserialize() { initialValue = defaultValue; }
public void OnBeforeSerialize() { }
}
SceneTransition script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneTransition : MonoBehaviour
{
public string sceneToLoad;
public Vector2 playerPosition;
public VectorValue playerStorage;
public GameObject fadeInPanel;
public GameObject fadeOutPanel;
public float fadeWait;
private void Awake()
{
if (fadeInPanel != null)
{
GameObject panel = Instantiate(fadeInPanel, Vector3.zero, Quaternion.identity) as GameObject;
Destroy(panel, 1);
}
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player") && !other.isTrigger)
{
playerStorage.initialValue = playerPosition;
StartCoroutine(FadeCo());
}
}
public IEnumerator FadeCo()
{
if (fadeOutPanel != null) { Instantiate(fadeOutPanel, Vector3.zero, Quaternion.identity); }
yield return new WaitForSeconds(fadeWait);
AsyncOperation asyncOperation = SceneManager.LoadSceneAsync(sceneToLoad);
while (!asyncOperation.isDone)
{
yield return null;
}
}
}