- Home /
How to save player position
I'm using a platform script but my problem is that upon exiting the Platform Trigger the player's Y position changes a lot. I'm trying to fix this by saving the player's position right before they leave the trigger and then reapplying it afterwards.
Here's my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlatformAttach : MonoBehaviour
{
public GameObject Player;
double PositionPlayer;
private void OnTriggerEnter(Collider other)
{
if (other.gameObject == Player)
{
Player.transform.parent = transform;
}
}
private void OnTriggerExit(Collider other)
{
PlayerPosition = Player.transform.position.y;
if (other.gameObject == Player)
{
Player.transform.parent = null;
transform.position = new Vector3(transform.position.x, PlayerPosition, transform.position.z);
}
}
}
Answer by harethl1s · Mar 18, 2020 at 01:24 PM
Try this code:
public GameObject Player;
// put those floats codes in the code when you want to save them like if you press a button
// this is the code of saving the values
float posX = Player.transform.position.x;
float posY = Player.transform.position.y;
float posZ= Player.transform.position.z;
If I try to put transform.position = new Vector3(transform.position.x, posY, transform.position.z); where my old code was then I get an error message saying Vector3 does not contain a definition for 'Y', any way I could fix this? (Sorry if this is a dumb question, I'm pretty new to Unity)