Unity and XR Interaction Toolkit: Cancel a teleport - unity3d

Unity 2021.3.16f1/URP 12.1.8
Creating an Escape Room-ish game. After they have solved all puzzles, a space will open to which they can teleport. The idea is that if they try to teleport there, instead of actually teleporting, I want them to get some sort of message they have finished the level.
So how can I stop them actually teleporting there? What I've done so far is create a Teleportation Area that only becomes active after all puzzles have been solved. In the "Teleportation Area" component, under "Interactable Events", I've set it up that whenever the "Teleporting" event is fired, my script is called.
So in this script, can I actually cancel the teleport? Or do I need to do it someplace else?
using UnityEngine;
using UnityEngine.XR.Interaction.Toolkit;
using UnityEngine.UIElements;
public class TeleportExtra : MonoBehaviour {
public void CancelTeleport( TeleportingEventArgs eArgs ) {
// Would like to cancel the teleport here. But how?
}
}

Related

Detecting Collisions in Unity?

I'm making a game in Unity where I want collisions between two moving objects to be detected (One of which is moved by the player using touch. For testing reasons I'm currently writing the script for mouse controls). However for some reason when the game object that is moving moves into the collider field of the object that needs to trigger an event when collided with, nothing happens. I added colliders to both objects and added Is trigger to the collider of the object that needs to trigger the event and as required but it still doesn't work.I tried it with the code that is supposed to trigger the event first and it didn't work and then I simply tried to use debug.log to see if the problem was with the code related to the event I want to get triggered but nothing works. Does anyone know how I might solve this problem?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Collision : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("Hit Detected");
}
}
You need to put a Rigibody on the 2 objects,
if your project is:
2D: "Gravity Scale" to 0 (so they don't have gravity)
3D: Set checkBox "Use Gravity" to false

Teleport Point: Switch to new scene

I have used the following site to set up the teleportation in my game: https://unity3d.college/2017/05/16/steamvr-locomotion-teleportation-movement/.
As seen from the image, i have inputted 'Location3' as the new scene i would like to teleport to in the build settings. However, when i run the .exe file, i am not able to teleport to a 'Location3' even though I am able to teleport to the other teleport points to a new location in the current scene.
The console logs the "TeleportPoint: Hook up your level loading logic to switch to new scene: Location3".
What you are trying to achieve seems to be a scene change (?)
This can be done using
Application.LoadLevel("Location3");
When you change to this scene you might want to manipulate the position of whatever it is that you want to teleport. This could be achieved by using a static class that does something depending on what scene you are changing too.
I do not recommend to use Application.LoadLevel(), because that function is obsolete and won't be supported in future Unity releases.
You can use:
SceneManager.LoadSceneAsync("NameOfScene");
In order not to keep your Teleportation script from being destroyed you can add the following script to your Teleportation Component.
[Serializable]
public class KeepOnSceneChange : MonoBehaviour {
public void Awake() {
DontDestroyOnLoad(this.gameObject);
}
}
Using this Component your object won't be destroyed when a new scene is loaded and therefor can be used to teleport.

Player dies when hitting the floor

I am trying to make a game in unity, and I am new to unity and coding, and I have started making a game, I have made some progress on it but I am having trouble finding some of my answers on youtube and the unity forum, and sometimes when I do, I still can't get things to work. So this is what I'm trying to do.
I have a map and the player is on top of the tower, I want the player to fall and when hitting the ground, dies with it displaying game over, What could I do to make this happen and what script?
So i have this now,
// Ground.cs: Kills players that touch this.collider.
using UnityEngine;
// Attach this to the grass collider
public class Ground : MonoBehaviour {
// Called when another collider hits the grass.
// This is part of Unity!
void OnCollisionEnter(Collision c) {
// Does the other collider have the tag "Player"?
if (c.gameObject.tag == "Player") {
// Yes it does. Destroy the entire gameObject.
Destroy(c.gameObject);
}
}
}
Now, I need to it transition to a game over overlay, which asks me to restart, yes or no.
The resources are out there in regards to learning Unity3D efficiently.
Look at the Unity3D tutorials: https://unity3d.com/learn/tutorials
These tutorials are usually kept up to date in terms of not using deprecated code. Additionally, several of the tutorials will teach you how to set up events like those that you need.
In regards to your immediate question though, look into forming the logic for your game.
You're going to need allow your player gameobject to fall via either gravitational force enacted on a rigidbody or hard-coded physics being applied to the gameobject. Next you will need to determine if the player gameobject has collided with the "floor". If so you will need to trigger an event to destroy the player gameobject, then display a GUI Text that says Game Over.
Look around more at tutorials and get better acquianted with Unity. Over time, you'll learn how to make things happen. If you have more questions, feel free to ask.
Answer to you update:
If your code is functioning correctly and your destroying your gameobject correctly, then awesome! You're learning fast!
The next step could be:
Create a Canvas, create a gui panel, create a gui text
That gui text object can have your Game Over Text
Now, create a button that you will utilize as the restart button
You can have the button call a function which utilizes SceneManager.LoadScene to reload the scene. Look here for an example: http://answers.unity3d.com/questions/1109497/unity-53-how-to-load-current-level.html
Next, Disable the panel as you will not need it until your player dies.
When your player collides with the ground you will destroy the player gameobject and set the panel you created to active. (you can trigger these actions via a listener in code or via the button component in the inspector).

In Unity Load another scene when the button is clicked on the first scene

I've created two scenes in unity of which I would like to load scene 2 If the button1 on scene one is clicked. I've been googling this issue,reading unity documentation but I only find this
Application.LoadLevel("SceneTwo");
I've tried to use this but my scene two just load without me clicking the button on the First scene.
Here is my code For the First scene at the moment
using UnityEngine;
using System.Collections;
public class first : MonoBehaviour {
// Use this for initialization
void Start () {
Application.LoadLevel("MiniGame");
}
// Update is called once per frame
void Update () {
Application.LoadLevel("MiniGame");
}
}
I've linked this script with the button on my First screen And "MiniGame" is the second scene.
Create GUI Button.
Write public function script for level changing. As in the following example.
using UnityEngine;
using System.Collections;
public class first : MonoBehaviour
{
public void GoToLevel(string level)
{
Application.LoadLevel (level);
}
}
Set your GUI Button's OnClick method. I used my script in canvas, you can use in any object.
Also you can watch unity scene selection tutorial
Application.LoadLevel (level); is obsolete.
Could use SceneManager.LoadScene()instead.
This is totally wrong usage of the Application.LoadLevel() functionality. But this is not the only problem here. Start() in Unity scripting is called once on the creation of the object which has this script attached to it. Update() is called every frame, which means that right now you are asking unity to load the 2nd level of yours thousands of times until it "crashes" i assume.
In order to fix this functionality you should create a clickable object by attaching this script to the object you want to click
using UnityEngine;
using System.Collections;
public class first : MonoBehaviour
{
// Use this for initialization
void Start ()
{
}
void Update ()
{
}
void OnMouseDown()
{
// this object was clicked - do something
Application.LoadLevel("MiniGame");
}
And of course you will need to add a collider to the object you are trying to click

Ending my 2D Game in Unity

I have been busy making a 2D game for the past month and am really happy with the way it has turned out... However my destroyer (collider) to end the level and send me to my other level which has the info on score etc isn't working how I would like it to..
Here the script on the Destroyer:
using UnityEngine;
using System.Collections;
public class EndGameDestroyer : MonoBehaviour {
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player") {
Application.LoadLevel(2);
return;
}
}
}
Im using C# btw
Currently I run through the level and see the Destroyer in the background but then it just disapears and doesnt end the level. Please help as I am showing my game to the public at a games expo my college is running tommorow...
Thanks in advance :D
Have you added the scene for level 2 to your build? If not you need to go to your build settings while in your level 2 scene and press "add current" below the scenes in build box. Then go back to whatever scene you were in previously to get to the trigger and see if that works.
first maybe you need to check is your player already in "player" tag
second i suppose your scene name 2
open your scene 2 and then file>Builds Settings and see if your scene 2 is listed in the scene list and checked if not then just click add current
and then
Application.LoadLevel("2");
it need string