Ending my 2D Game in Unity - unity3d

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

Related

How to animate grabbing in Unity VR?

I've been googling this for 10 hours and I'm running out of ideas. I found several video-tutorials and written-tutorials but none of them really works or they're overkill.
I have VR Unity (2020.3.16f) project designed to be run on Quest 2. I'm not using OpenXR. I already created hand, created one simple grabbing animation, added animation to Animator, created transitions, and set "IsGrabbed" parameter. Now I'm looking a simple way to change "IsGrabbed" to true/false whenever I grab/release anything. I'm expecting something like this:
public class grabber : MonoBehaviour
{
// Start is called before the first frame update
Animator animator;
???
void Start()
{
???
}
// Update is called once per frame
void Update()
{
if (???)
{
animator.SetBool("IsGrabbing", true);
}
elseif (???)
{
animator.SetBool("IsGrabbing", false);
}
}
}
Please help. We're talking about VR here so I'm sure grabbing animation is the very basics of very basics of very basics. It can't be any difficult.
Best regards
First of all, I highly recommend watching this video by Justin P Barnett to get a much better overview of how this all works.
If you don't want to go that route for some reason, there are other options available to you. One such option is the "Player Input" component, which can act as a bridge between your input devices and your code. Most XR packages these days use the new Input System package, and it makes life easier, so I will assume you have that installed.
First, you will need to create an Input Actions asset, which can be done in the project pane: right-click -> Create -> Input Actions. There are many tutorials which explain this asset in detail, but here is a simple setup to get you started. Double click on the new asset to open the editing window, and create a new Action Map. In the "Actions" list, create a new action with action type Value, Control Type Axis, and in the dropdown arrow on your new action set the path to the input source. As an example source path, I will use XR Controller -> XR Controller -> XR Controller (Left Hand) -> Optional Controls -> grip. Make sure to click Save Asset before closing the window.
Create a script similar to this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class ControllerInputReceiver : MonoBehaviour {
public void FloatToScale(InputAction.CallbackContext context) {
float val = 0.1f + 0.1f * context.ReadValue<float>();
transform.localScale = new Vector3(val, val, val);
}
}
Create a cube somewhere visible in your scene, and add the Input Action Manager component to it, and drag your created Input Actions asset to its list of Action Assets. Then add the ControllerInputReceiver script. Also on this cube, create a Player Input component and drag your Input Actions asset to its Actions element. Choose your map as the default map and change behavior to Invoke Unity Events. Under the events drop down, you should see an element for the Action you created earlier. Drop your Controller Input Receiver component into this Action and select the FloatToScale function.
In theory it should work at this point. Build the game to your device and see if pulling the grip causes the cube to resize. If it does, then you can replace your Update function with:
void SetGrabbing(InputAction.CallbackContext context) {
animator.SetBool("IsGrabbing", context.ReadValue<float>() > cutoff);
}
If you are still having issues at this point, I really recommend checking out these youtube channels. I only started VR a couple of months ago and learned everything I know so far from these people. JustinPBarnett, VRwithAndrew, ValemVR, LevelUp2020. (Links removed because it kept screwing up my post)
Note, the new input system has button options instead of value/axis options for VR devices. These may be closer to what you want, but I had no luck getting them to work today.
Also note, depending on how you organize your code, you may or may not need the "Input Action Manager" component somewhere in your scene with your input actions asset in its list. It enables your actions for you, without you needing to do this programmatically.
Another solution would be:
Using the OVR plugin and Hands prefab (for left and right), to check whether the rotation of each of the fingers on a specific axis (ex. Z-Axis) falls under a specific range-meaning fingers are grasping/flexed.
By referencing for example the Transform of b_l_index1 , which is the corresponding part of the Index Finger within the model, you can check its local roation every frame and trigger the event of grasping when all fingers are rotated to a specific angle. Subsequently, triggering the animation you need.

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.

Unity Game Stops On Load Same Scene Again

I have 7 scenes in my game. one of the scenes is "Playing" scene witch is my game play scene. in this game, one match includes 3 round. i save some of match data in "MatchManager" script that attached to MatchManager game object that have a singleton and don't destroy on load. other info are in a script(PSceneManager) that attached to a gameObject(PSceneManager) belong to "Playing" scene.
In the end of each round i change some data and then I Have To Load "Playing" scene again.
HERE IS MY PROBLEM: when it loads again it stops at very beginning and even don't enter "start" function (method).
(I have a singleton in PSceneManager scrip. I delete that and even check and looking for any other static value.(there were no more static variables))
I HAVE NO IDEA WHAT IS MY PROBLEM??!(any idea can be helpful)
HEEELP PLEASE...I'M STUCKS HERE...
I don't know exactly what the problem is. Some more information would be helpful.
If I would have had this problem I would look for the first and last moment the script still works.
Unity has already build this in for us. Just like Start() and Update() we have OnEnable() and OnDisable(). So if you add a Debug.Log() statement in those MonoBehaviour functions you know if the script and object are still active.
void OnEnable() { Debug.Log( gameobject.name + " is enabled" ); }
void OnDisable() { Debug.Log( gameobject.name + " is disabled" ); }
https://docs.unity3d.com/Manual/ExecutionOrder.html
I have faced the same problem, and I found that before the loading of the other scene again I changed the Time.timeScale to zero.
I solved the problem by reassigning its value in the start of any game object of the scene that stops.

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).