- Home /
Save player position when trigger is destroyed
Hello everyone, this is a little bit of a loaded question, but i have looked online for some time and have been unable to find any answers. Hopefully you guys can help.
So, what I am trying to do is have the game save the players position after the player enters a trigger, but after the trigger is destroyed. I am not sure how to get this is call properly.
That is the first part, the second question I have is the complicated issue… at least is seems so as I can’t find anything about it: I am making the game in 1 scene, and when the player enters the trigger is loads the next section of the scene and removes the section they are not currently in. What I want to know is if the scrip I have saving the players position will save the section that have been removed.
Here is the script I have
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
Public class Tigger : MonoBehaviour
{
public GameObject Trigger;
public GameObject Animator;
public GameObject player;
public float x;
public float y;
public float z;
Private void Start ()
{
if (PlayerPrefs.HasKey ("x") && PlayerPrefs.HasKey ("y") && PlayerPrefs.HasKey ("z"))
{
x = PlayerPrefs.GetFloat("x");
y = PlayerPrefs.GetFloat("y");
z = PlayerPrefs.GetFloat("z");
Vector3 posVec = new Vector3(x,y,z);
player.transform.position = posVec;
}
}
Private void OnTriggerEnter ()
{
Animator.GetComponent.<Animation>().Play("Music down");
}
Private void OnTriggerExit ()
{
Destroy(Trigger);
}
void Update ()
{
If () //This is where I was going to have the script recognize the trigger is destroyed. Not sure how to have it be called
{
x = player.transform.position.x;
PlayerPrefs.SetFloat("x", x);
y = player.transform.position.y;
PlayerPrefs.SetFloat("y", y);
z = player.transform.position.z;
PlayerPrefs.SetFloat("z", z);
}
}
}
Thank you for any assistance.
Answer by TreyH · Apr 13, 2018 at 08:24 PM
C# uses lowercase for private
and public
. For your problem, you're already checking when an object is hit. Just do it there?
void OnTriggerExit (Collider other) {
Destroy(other.gameObject);
PlayerPrefs.SetFloat("x", player.transform.position.x);
PlayerPrefs.SetFloat("y", player.transform.position.y);
PlayerPrefs.SetFloat("z", player.transform.position.z);
}
I typed it all in word for mobile. that is why it is not in lowercase. Thank you for answering. what about the other part of my question?
If your other question is about whether or not it will remove the old "section", then we can't really answer that. This will save that location of your player, which (by your Start function) will be their position the next time this runs.
ok, thank you. and just a tip, when helping people with issues, attitude doesn't really help. but thanks.
Your answer
Follow this Question
Related Questions
Save values on different objets with same script? 1 Answer
PlayerPrefs don't work 2 Answers
Adding Value to Already Saved Values 1 Answer
Flinging Wall Death 1 Answer
How to check int value of PlayerPref 1 Answer