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.
Related
I created a project using the FPS Microgame in Unity Hub to learn scripting. I'm trying to play a simple particle effect when the player collides with it (in the future I'd love to be able to trigger it from farther away, maybe when crossing into a plane, but I'll learn that later). I created the following script and attached it as a component to the particle effect on the scene:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class portalTrigger : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
ParticleSystem ps = GetComponent<ParticleSystem>();
ps.Play();
}
}
And here's what the particle effect looks like in the Unity Inspector:
The particle effect plays when the game starts if I select Play on Wake so I disabled it because I want it to play when the user collides with it. Any ideas?
Thanks!
First I would add a debug print to verify that a collision is triggered in the onTriggerEnter method.
If that is not the case, I would check that both game objects have a rigid body and a collider. One (and only one) of the two colliders must have the isTrigger flag set.
Here is unity doc link for reference:
https://docs.unity3d.com/ScriptReference/Collider.OnTriggerEnter.html
Had you quite made sure that the isTrigger option in object main object has been checked true?
If not then either see the way to check it( What do I mean here is that if you only check isTrigger without making another collision than object won't stop even after the collision. It had been really hectic for me when I started.)
or use the OnCollisionEnter. If none of these happened then try printing to see if they're even colliding or not. C'mon you are trying to become a Unity scripter so best way is to debug anything you doubt about.
I'm using Unity 2020.3.21f1, Universal Render Pipeline
Hi, I'm new to this, I'm adding a second camera to my scene through using 'Don't Destroy on load'
The Previous scene (Scene1) Has this:
void Start()
{
SceneManager.LoadSceneAsync(LoadingData.sceneToLoad);
//Static script called
}
And the menu and camera items in this scene have this attached:
void Start()
{
DontDestroyOnLoad(gameObject);
}
So when Scene2 loads, there will be a main camera, and Scene1's Camera. However, in the game mode, when I rollover the (Scene2) main camera's stack, it says it cannot detect an overlay camera, even though the Scene1's overlay camera is in the Scene2 Hierarchy?
When I manually copy over the Scene1 Overlay camera to Scene2, the stack can detect it, and everything works perfect. It is only when using don't destroy on load that it cannot detect a camera?
This might be unrelated, but I am trying to use:
public void CheckForCamera()
{
var cameraData = GetComponent<Camera>().GetUniversalAdditionalCameraData();
cameraData.cameraStack.Add(OverlayCamera);
}
To add the camera, but even with this, when I roll over the main camera stack, it says overlay camera cannot be detected, so I'm guessing I need to figure out why it can't be detected first before this script can be tested.
I have also made sure their tags are different. Scene1 Camera has the tag 'HUDCamera' and Scene2 Main Camera has the tag main camera
Scene1 Camera (HUD Menu Script contains just don't destroy on load):
Scene 1 Camera Inspector Details
Scene2 Camera (Camera Controller Script follows player around)
Scene2 Camera Inspector Details
I've been stuck on this for ages, someone please help!
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.
I am making a simple VR Tour in Unity, and I want to move the camera to the next sphere (or at all at this point) when I click on a sprite that is hovering in the air. Right now, when I run this code, I get the "Sprite Clicked" message in the console, but the camera doesn't move at all. Any help is greatly appreciated!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DebugOnClick : MonoBehaviour {
// Use this for initialization
void Start () {
}
void OnMouseDown () {
Debug.Log("Sprite Clicked");
Camera.main.transform.position = new Vector3(5.0f, 5.0f, 5.0f);
}
}
It doesn't let me comment (not enough rep) so I must pop this in as an answer.
If you're using some given VR scripts already, it could be possible that they're syncing the camera position constantly to where they should be relative to the player's headset position.
Meaning that after this move via script, the camera will be moved back instantly.
If that is the case, you need to move the player avatar themselves to the correct position rather than the camera.
I'm new to unity and I'm currently working on a portal-like game.
I did the whole teleportation script and it works, but the problem comes that I didn't implement the player camera correction and actually I don't have any ideas how to do it. The concept is that when you're jumping through a portal, the player (or player camera) rotation should be changed to the portal/portal camera rotation from you've come so the final effect is more 'realistic'.
I've tried some lines in teleportation script like player.transform.rotation = portal.transform.rotation but in the end it didn't work and now I end up with nothing, deleting previous scripts and trying to write it all over and over again.
I'll be glad if someone could guide me how to start coding it. Should I do it in onTriggerEnter (when you're jump through portal), or in onTriggerExit? Should the script be attached to a player or to a portals? Should I gather rotation only from camera or from the whole gameobject (portal/player)? I'm posting also couple of screens (with a video how it currently works, and also an entire teleportation script. If I missed something just ask me and I'll post it here.
https://imgur.com/a/pbqYnLD - screens with portals inspector
https://streamable.com/b14hk - video how it works
teleportation script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Teleportation : MonoBehaviour {
[SerializeField] private GameObject otherPortal;
[SerializeField] private GameObject player;
void OnTriggerEnter(Collider col) {
if(col.tag == "Player") {
col.transform.position = new Vector3(otherPortal.transform.position.x+1, otherPortal.transform.position.y+1, otherPortal.transform.position.z+1);
Debug.Log("wszedłem w portal");
}
}
void Update() {
}
}
some informations how it is coded right now:
portals are currently in game behind 'the box', i didnt instantiate them anywhere; just changing position on lpm (blue portal) and ppm (orange portal)
portals are sticking to the walls, just like in the original game
portals have a camera attached to it and right now the cameras are static. (offtop: i have a script to move them exactly when player is moving and it quite works but also have some problems, like camera can get too far away from portal and start rendering only that green outer side of the box, and i also dont know how to fix it, so currently i didnt use this script)
the player movement im using is that from unity standard assets (if it somehow matters)
the player have a rigidbody but the portals dont; not sure if i should attach this component to them
teleportation script is attached to the both of portals - the 'otherPortal' variable is moved from inspector, like in orange portal the 'otherPortal' variable is blue portal and the other way
What you did is correct (setting the player rotation to the portal.
You can do it in onTriggerEnter after setting the position, then it should look like
player.transform.rotation = otherPortal.transform.rotation
If you do that, the player will have the same rotation. You already have something that make the camera follow the player, so it is likely that you don't need to set the camera rotation. I don't know how you did your camera follow, so I can't be sure, though. If the camera has not the proper orientation, doing Camera.main.transform.rotation = otherPortal.transform.rotation will do it.
The remaining thing that might be wring, could be that your player (and camera) is not facing the right axis. On your video, I can see that the portal faces the x-axis (the red axis in Unity editor). Check that when going forward, your player has the red axis looking forward.
It is likely that your player has the z-axis (blue) facing forward, which is (by convention) more correct and fits the names Unity uses (z-axis is also called forward-axis)
I would recomand to create the portal object (and all other objects, including the player) so that the forward-axis is the blue one. It might require editing the objects. Anycase, check that the player forward axis is that same as the portal, otherwise setting the rotation won't work