The question is answered, right answer was accepted
Unty 3D C# Load other scene when bool = true, otherwise perform teleportation as commanded.
Hello everyone,
I got another question I wanted to ask.
I already got a teleport to target location when you exit one, so that is done. But what I want to achieve now is that when you have interacted with some object, that then The bool is set on true, and when you collide with the object this script is attached to you will be send to another scene, otherwise you will just be teleported to the beginning of this scene.
this is the script I assembled :
using UnityEngine;
using System.Collections;
public class Telepiti : MonoBehaviour {
/// <summary>
/// Here I take the bool of if I am teleported, so that I can prevent spam porting.
/// Target = the point where I will be teleported to.
/// </summary>
public bool Interacted = false;
public Interacted interact;
public bool teleported = false;
public Telepiti target;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
///<summary>
///If I am teleported then It will be set to true and I will be teleported to point B
///Then It sets the teleport on false AND I will be able to teleport again!
/// /summary>
if (other.CompareTag("Player"))
{
if (!teleported)
{
///<summary>
//Teleports me to Location B AND turns me to a chosen degree with quaternion.Euler</summary>
target.teleported = true; other.gameObject.transform.position = target.gameObject.transform.position;
other.transform.rotation = Quaternion.Euler(0, 0, 0);
}
}
}
/// <summary>
/// This gives me the possibility to teleport
/// </summary>
/// <param name="other"></param>
void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
teleported = false;
}
}
}
Now I have walked into a wall, and I can not get past it. So what should I do to be able to Teleport to another scene IF the bool is set to true because I interacted with something? Should I make another void with onCollisionEnter? I am making an RPG horror game (something like Silent hill.). I want it to teleport you to another scene IF the bool = true, and if you did not interact with an object and collide with the Telepiti, then you will be teleported to Point B in the same scene.
I just do not know what I should do.
Thank you all again in advance for all your help, And have a nice day,
Daniel Nowak Janssen
Answer by Socapex · Jan 19, 2016 at 06:57 PM
if (other.CompareTag("Player"))
{
if (potato) {
Application.Load("MyLevel2");
}
if (!teleported)
{
target.teleported = true; other.gameObject.transform.position = target.gameObject.transform.position;
other.transform.rotation = Quaternion.Euler(0, 0, 0);
}
}
You could use else if, but it doesn't matter as the level will be destroyed and it wont continue checking further.
I got this :
Script 1 :
using UnityEngine;
using System.Collections;
public class Telepiti : $$anonymous$$onoBehaviour {
public bool teleported = false;
public Telepiti target;
public bool Interacted;
// public Interacted interact;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Player"))
{
if (Interacted ==true)
{
Application.LoadLevel("2");
}
if (!teleported)
{
target.teleported = true; other.gameObject.transform.position = target.gameObject.transform.position;
other.transform.rotation = Quaternion.Euler(0, 0, 0);
}
}
}
void OnTriggerExit(Collider other)
{
if (other.CompareTag("Player"))
{
teleported = false;
}
}
}
Script 2 :
public class NextLevelInteraction : $$anonymous$$onoBehaviour {
GameObject mainCamera;
public float interactDistance = 1f;
public Telepiti Teleportation;
void Start () {
mainCamera = GameObject.FindWithTag("$$anonymous$$ainCamera");
GameObject G = GameObject.FindGameObjectWithTag ("Special");
Teleportation = G.GetComponent<Telepiti>();
Teleportation.Interacted = false;
}
// Update is called once per frame
void Update () {
}
void pickup()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.E))
{
int x = Screen.width / 2;
int y = Screen.height / 2;
Ray ray = mainCamera.GetComponent<Camera>().ScreenPointToRay(new Vector3(x, y));
RaycastHit hit;
if (Physics.Raycast(ray, out hit, interactDistance))
Debug.Log("Touching your special things.... huehuehue");
{
SpecialPick p = hit.collider.GetComponent<SpecialPick>();
Teleportation.Interacted = true;
if (p != null)
{
Debug.Log("magic!");
}
}
}
}
}
And somehow he does NOT want to change interacted to true...
What Have I done wrong?