- Home /
Why does my player go to a random position after setting its position to a specific Vector3 and loading a level?
Hi, so in my game I have one doorscript which I put on all door objects so when they are activated they should send the character to a certain position (which i manually input as a public Vector3) and a certain level (which I also manually input as a string) all through the inspector
heres the gist of the script:
using UnityEngine;
using System.Collections;
public class DoorScript : MonoBehaviour
{
GameObject player;
public bool unlocked;
public int Key;
Spells spells;
public bool LeadsToDifferentScene = false;
public string LevelItLeadsTo;
public Vector3 LocationItLeadsToInThatLevel;
public bool quantumLeap = false;
// Use this for initialization
void Start()
{
player = GameObject.FindGameObjectWithTag("Player");
spells = player.GetComponent(typeof(Spells)) as Spells;
}
public AudioClip lockedsound;
public AudioClip opensound;
// Update is called once per frame
void Update()
{
if (tag == "Active" && unlocked)
{
AudioSource.PlayClipAtPoint(opensound, transform.position);
if (!LeadsToDifferentScene && !quantumLeap)
{
player.transform.position += player.transform.up * 1f;
}
else if (LeadsToDifferentScene && !quantumLeap)
{
player.transform.localPosition = LocationItLeadsToInThatLevel;
Application.LoadLevel(LevelItLeadsTo);
} else{
player.transform.position = LocationItLeadsToInThatLevel;
}
tag = "Inactive";
}
else if (tag == "Active" && !unlocked)
{
if (spells.LookForKey(Key))
{
unlocked = true;
// play unlocke sound?
AudioSource.PlayClipAtPoint(opensound, transform.position);
if (!LeadsToDifferentScene)
{
player.transform.position += player.transform.up * 1f;
}
else if (LeadsToDifferentScene && !quantumLeap)
{
player.transform.localPosition = LocationItLeadsToInThatLevel;
Application.LoadLevel(LevelItLeadsTo);
} else{
player.transform.position = LocationItLeadsToInThatLevel;
}
tag = "Inactive";
}
else
{
AudioSource.PlayClipAtPoint(lockedsound, transform.position);
GameObject ohno = Instantiate(Resources.Load("Messages/OkMessage")) as GameObject;
OkMessage ohyes = ohno.GetComponent(typeof(OkMessage)) as OkMessage;
ohyes.message = "Locked";
tag = "Inactive";
}
}
}
}
On this particular door I have unlocked = true, leadstodifferentscene = true, levelitleadsto = MyLevelName, locationitleadstointhatlevel = the vector <17, 35, 0>, quantumleap = false.
It literally works for all of my ~20 doors that are in the game so far! For some reason this door isn't working and it is sending the player to the position <4.92, 19.2, 7.14> a seemingly completely RANDOM position!!?
Is it because the level isn't loading properly.. or what? This door is exactly the same as this other door that leads to <6, 36, 0> in the SAME EXACT SCENE and it works perfectly.
ALSO I used player.transform.localPosition because I tried it with player.transform.position thinking it would fix it, but it doesn't, it's the exact same result. The player object is not a child to anything so it shouldn't.
any ideas would be nice! thanks
Noob question. I am curious as to where the "Spells" constructor come from?
spells is just a script / class I wrote and put on the player and it contains all item and spell info. This script just gets that and uses its LookFor$$anonymous$$ey method to see if the player has the key for the door (spells for me contains items as well)
Answer by charblaze · Jul 10, 2015 at 08:38 AM
Hi actually I just figured out what is causing this... I was using the "dontgothroughthings" script i found online and it turns out my door was transporting me such a large distance it was glitching me out because of thsi script. So i just disabled it and now it works perfectly!! Sometimes the answer to your problem is completely unrelated to what u think the problem is.
Your answer
Follow this Question
Related Questions
My character's y position gets reset on play. 0 Answers
How do you make a player move on a remote items local axis? 0 Answers
How to spawn objects on a restricted sphere surface relative to camera? 1 Answer
Setting Player positioning on a different loaded scene with Dont Destroy On Load attached. 0 Answers
Getting and sending a gameobjects position to another gameobject. 1 Answer