This is the current script that I have. I have a ball that is player, and I want a collision with my game object tagged as "pit" to end the game. When the game ends I want my game over canvas to pop up. The current script detects the hit when the player rolls over the pit, however, currently, nothing else happens. Both my player and pit have rigidbodies and colliders attached to them. I would really appreciate any help with how to get my code to work properly.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Trigger : MonoBehaviour
{
public GameObject GameManager;
private void OnTriggerEnter(Collider other)
{
Debug.Log("hit detected")
if(other.gameObject.tag == "pit")
{
GameManager.GetComponent<Game_Manager>().EndGame();
}
}
}
My EndGame code is in my GameManager script. The is the part of that script that deals with ending the game.
public void EndGame ()
{
player.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.FreezePosition;
hasGameStarted = true;
inMenuUI.gameObject.SetActive(false);
inGameUI.gameObject.SetActive(false);
gameOverUI.gameObject.SetActive(true);
}
If I'm correct in interpreting your question, your script successfully detects player death already. If you want a game over screen you have a couple of options:
You can create a UI canvas with a panel on it that obscures the whole screen and put the words Game Over.
Or, probably the better option is to create an entirely new Unity scene and make the Game Over canvas there. When you want to trigger the game over screen you simply use:
Scene manager.LoadScene("Scene name");
You have to add "using UnityEngine.SceneManagement;" at the top of your script. Also note you have to add your Game Over scene to the list of scenes in you game. I believe this can be done by navigating to File>Build settings>scenes and then pressing add open scenes assuming the game over scene is open.
Related
I have a problem, I'm making a 2d game in unity and i've create a scene that is load at the begining where my player is load, and then it sends it into the scene "salleA". I started by trying to lauch the game from this scene without adding a box collider for the walls of salleA, it worked. Then I added a box collider for the walls and it sends me to salleB, with a really strange display. Please I'm a beginner I don't understand. Here are the pictures :
salle A :
salle B :
and finally the strange display, when game is lauch but from the scene view :
I precise that the black squares are box collider trigger. The right one of salleA make you go to salle B and the left one of salleB make you go to salle A.
code of the first scene where the player is create :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class changeSalle : MonoBehaviour
{
public Collider2D player;
// Start is called before the first frame update
void Start()
{
SceneManager.LoadScene("salleA");
DontDestroyOnLoad(player);
}
}
the collider of the player : (capsule)
the collider of the salleA : (edge)
After more searches, I think that the problem is more difficult. I tried to put a camera in the sceneA and lauch the scene, it sends me to salleB with the message "no camera rendering", that is normal because the camera is a child of player that isn't destroy on load of each scene. So I tried to remove the door to Salle B, I lauched and it sends me to salleC, the scene that you can reach by the left black square. Then I removed the door to salleC and I lauched. It lets me in the SalleA, but I only see yellow. Picture :
I think nothing else is important, if you need something else tell me in the comments.
Thanks, Ilou.
It now works, I tried so many things and I recreated the collider, I sized it to make it touch no other possible collider, like the sellers' collider, and it's good. I think that unity doesn't like when you overlap colliders.
So I've just been tinkering with unity and all yesterday I was making a little game. I have a main menu when you start the game, the game itself and a gameover menu when you die that puts you back at the home screen, but I have no clue how to set it when a thing/enemy hits the player, It brings you to the restart menu on collision with it. anyone know any code to help out, I've been trying to find videos but they are not what I need. I need when on collision set scene to GAMEOVER. But don't know how, PLZ help.
So to what I understand you want to make a collision and restart you scene or go to another scene ?
You need to use something like this.
I added 3 possibiities of Scenemanager.LoadScene however you need to use them.
make sure to add the the using UnityEngine.SceneManagement; at the top
using UnityEngine.SceneManagement;
void OnCollisionEnter(Collision collision)
{
//if you want to load another scene
SceneManager.LoadScene("OtherSceneName");
//if you want to load the same scene you are on
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
//if you want to load the same scene you are on
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
To know if an object with a Collider was hit in Unity (e.g. the player) you need to add one of the OnCollision functions to your script:
OnCollisionEnter - is called when the object is first hit
OnCollisionStay - is called every frame while the object is being touched
OnCollisionExit - is called on the frame the object stops being touched
Then, in the function you can load a different scene using SceneManager.LoadScene
I'm trying to create a script in Unity wherein the gravity will go to zero when the game starts. The problem is, the gravity was still there after I launched the game.
This is for a Unity3D game. I've already tried to change the variable name, add using System; and moved my script to the top of the Rigidbody component, but none of these things worked.
public class PlayerGrav : MonoBehaviour
{
public Rigidbody rb;
void Start()
{
rb.useGravity = false;
}
void Update()
{
}
}
I expected the gravity to be removed when the game launches, but in the actual output the gravity still remained, and the cube still fell downwards.
create a new scene
create a new cube
add rb = this.GetComponent<Rigidbody>() to your start function in your PlayerGrav script
attach your PlayerGrav script to the new cube
Hit play, and take look at the "use gravity" property from your new cube's inspector window (make sure the inspector isn't locked to other game object)
If the above tryout succeeds, then you need to check the other code/objects in your original scene. If there are massive other objects in your original scene, then I will suggest disable other objects first, and re-enable them back group by group to see which object/monobhevaiour is causing the issue.
I know this is an old question but I looked forever trying to find an answer to this. I am new to unity so it took me a long time to figure this out but I am using a Rigidbody 2d so this is what worked for me
gameObject.GetComponent<Rigidbody2D>().gravityScale = 0;
Right now, I have a multiplayer game where the player fires projectiles at other players that have a certain amount of bubbles. Once a player has lost all of their bubbles, they lose.
The issue I had previously was that on one screen, a first player could shoot out a second player's bubbles, but not all of the bubbles on the second player's screen would be popped, so I am trying to sync it over the network somehow.
The issue I'm seeing is that NetworkServer.Destroy requires finding the GameObject you intend to destroy by its NetworkIdentity, but only the root Player GameObject is allowed to have the NetworkIdentity component on it. What's the best way I can sync the destruction of a child object of a player?
The https://unity3d.com/fr/learn/tutorials/topics/multiplayer-networking/ course is very similar to the game you describe. It has a Bullet script attached to the fired bullets. If the bullets touched something, it check if it's a player, and apply damage to it (the player prefab has a Health script attached to it).
using UnityEngine;
using System.Collections;
public class Bullet : MonoBehaviour {
void OnCollisionEnter(Collision collision)
{
var hit = collision.gameObject;
/* Code below is from the original example
var health = hit.GetComponent<Health>();
if (health != null)
{
health.TakeDamage(10);
}*/
var bubble = hit.GetComponent<BubbleBehavior>();
if (bubble != null)
{
bubble.Popped(); // Implement a Popped function doing the magic
}
Destroy(gameObject);
}
}
In your case you can do the same, but instead of checking if the touched object is a player, simply check if it is a bubble, and do whatever you want from the touched bubble.
In the original example, they sync the health (it is a "SyncVar"), in your case you can sync the number of bubbles remaining.
in advanced I would like to say if this is a really simple question with a simple answer, I apologize as I have just now gotten into programming.Basically, I'm trying to create a script that a block named blue(picture below) on collision with the FPSController, will get destroyed, here is my script:
using UnityEngine;
using System.Collections;
public class Cube : MonoBehaviour {
void OnCollisionEnter (Collision col) {
if(col.gameObject.name == "Blue") {
Destroy(col.gameObject);
print ("collison detected");
}
}
}
for some reason, though, whenever the fps controller collides with the object known as "Blue" nothing happens, the print() function is not triggered nor is the destroy() function
Thank you in advaned ;)
Rigidbody` is missing from your Cubes.
1.Attach Rigidbody component to both cubes.
2.Also,set both Cubes Rigidbody to Is-kinematic. You must set both Cubes Rigidbody to Is-kinematic so that the Character Controller wont be able to move it. Note that if your cube is falling down after adding Rigidbody, simply disable Use Graivty on the Rigidbody.
Important:
3.Delete FPSController. Since you will be interacting with other Rigidbody GameObjects, use RigidBodyFPSController. It can be found in Assets\Standard Assets\Characters\FirstPersonCharacter\Prefabs. Drag RigidBodyFPSController to the Scene then attach Cube script to it.
You will notice there is a Rigidbody attached to RigidBodyFPSController. Don't modify the settings of it.
That's it. Everything should be working as expected.
Cube settings:
RigidBodyFPSController Settings: