Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Ejpj123 · Apr 18, 2016 at 12:23 PM · collision2d gamecamera-movementload leveledge detection

Load Scene on Either Edge of Camera Collision

Hi everybody, I am making a 2D infinite runner game, and the Player has to keep running to the right, because the camera is also going to the right, and I am trying to make it so that when the player touches the edge of either side of the screen, it will load a Game Over scene, and I don't know how.

Thanks for the help!

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ejpj123 · Apr 18, 2016 at 11:01 PM 0
Share

Someone Please Help!!

avatar image Glurth · Apr 18, 2016 at 11:24 PM 0
Share

$$anonymous$$gest you show us what you have tried so far, and where it fails, or where you get stuck. This general gets better responses.

avatar image Ejpj123 · Apr 19, 2016 at 01:41 AM 0
Share

Well, I tried to make it so that when the player touches a platform, on the left side of the camera, which the platform is following the camera and that when the player touches the platform, it loads a Game Over scene. Here is the script:

using UnityEngine; using System.Collections; using UnityEngine.Scene$$anonymous$$anagement;

public class DestroyerScript : $$anonymous$$onoBehaviour {

 void OnTriggerEnter2D(Collider2D other)

 {
     if (other.tag == "Player") 
     {
         Debug.Break ();
     }

     if(other.gameObject.name == "RedGiant") 
     {
         Application.LoadLevel ("Game Over");        }

     else 
     {
         Destroy (other.gameObject);
     }
 }

}

$$anonymous$$y game over scene is called Game Over. Nothing happens when I press play. There are no errors. But then I figured out that the player can collide with the camera, some how, and I don't hav any script for it and don't know what to do

avatar image skylem · Apr 20, 2016 at 04:26 AM 0
Share

You haven't specified whether your game is 2D or 3D this makes it very difficult to give u an appropriate answer, if the game were 3D i would place Spherecasters at the end of all my levels 2D and i would place overlap circles ins$$anonymous$$d.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Johat · Apr 20, 2016 at 06:16 AM

Hi.

One approach to this, is just to add a BoxCollider or similar attached to the camera just off of the edge you want to trigger the game over.

This may as well just be a trigger than a full collision. (You'll need to make sure either the player or the collider has a RigidBody attached to it -- you'll likely want to check the box that says "Kinematic" and you'll probably want to uncheck the box that says "Use Gravity" too.)

Then you could attach a script such as this to the trigger:

 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class GameOverTrigger : MonoBehaviour
 {
       public string GameOverScene;
 
       private void OnTriggerEnter(Collider other)
       {
             if (other.CompareTag("PLAYER_TAG")
             {
                   SceneManager.LoadScene(GameOverScene);
             }
       }
 }

Of course, you could do it the other way around too and check if the wall hit the player (rather than the player hitting the wall).

NOTE:

Not sure if you have 2D or 3D Colliders... If you have 3D, the above will work. But for 2D Colliders, you need to use OnTriggerEnter2D instead, with the parameter type being Collider2D.

(This isn't to do with whether your gameplay is 2D or 3D, rather what type of Colliders you have attached to your GameObjects.)

Personally, I would be tempted to forego the manual setup of a trigger and just check the actual condition I care about: "did the player leave the left of the screen?". WorldToScreenPoint or similar would be ideal for this.

Something like this attached to your Camera should do that:

 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class GameOverCheck : MonoBehaviour
 {
       public string GameOverScene;
 
       // You could automatically find this in Start or Awake if you preferred
       public Transform Player;
       // How far ahead of the player's position (typically the centre) we should check to be off-screen
       public float CheckOffset;
 
       // We need to have a reference to the Camera component
       private Camera _camera;
 
       private void Start()
       {
             _camera = GetComponent<Camera>();
       }
 
       private void Update()
       {
             // This just is just to check a little further ahead of the player's position, since this position is normally the centre and you probably want to player to be completely off-screen
             // This assumes the player's right is pointing in the direction they are moving, but if this isn't the case you might need to change this

             // Let's store this world offset in a local variable
             Vector3 worldOffset = CheckOffset * Player.right;

             // Calculate the left screen position to check
             Vector3 leftPositionToCheck = Player.position + worldOffset;
             Vector3 leftScreenPosition = _camera.WorldToScreenPoint(leftPositionToCheck);

             // Calculate the right screen position to check
             // This time we want to offset to the left to make sure the player is completely off-screen to the right
             Vector3 rightPositionToCheck = Player.position - worldOffset;
             Vector3 rightScreenPosition = _camera.WorldToScreenPoint(rightPositionToCheck);
 
             // screenPosition is in pixels, where x (along the width of the screen) goes from 0f at the very left to the width at the very right
             // If the x value of the left position we're checking is less than 0f, then we're definitely off-screen!
             // Also, if the x-value of the right position we're checking is greater than the screen width, then we're definitely off-screen!
             if (leftScreenPosition.x < 0f || rightScreenPosition.x > Screen.width)
             {
                   SceneManager.LoadScene(GameOverScene);
             }
       }
 }

Either should work fine. Let me know how it goes =).

(Here's the reference for Screen.width. You may also be interested in Screen in general, to see what else is available to you.

(Excuse any typos, I'm on a tablet.)

Comment
Add comment · Show 9 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ejpj123 · Apr 23, 2016 at 09:15 PM 0
Share

There are errors (in game exceptions) WHILE the game is running. Errors like this :

 $$anonymous$$issingComponentException: There is no 'Camera' attached to the "Patch Pirate" game object, but a script is trying to access it.
 You probably need to add a Camera to the game object "Patch Pirate". Or your script needs to check if the component is attached before using it.
 UnityEngine.Camera.WorldToScreenPoint (Vector3 position)
 DestroyerScript.Update () (at Assets/DestroyerScript.cs:27)

and this

 UnassignedReferenceException: The variable Player of DestroyerScript has not been assigned.
 You probably need to assign the Player variable of the DestroyerScript script in the inspector.
 UnityEngine.Transform.get_position ()
 DestroyerScript.Update () (at Assets/DestroyerScript.cs:25)
 

and this

 $$anonymous$$issingComponentException: There is no 'Camera' attached to the "RedGiant" game object, but a script is trying to access it.
 You probably need to add a Camera to the game object "RedGiant". Or your script needs to check if the component is attached before using it.
 UnityEngine.Camera.WorldToScreenPoint (Vector3 position)
 DestroyerScript.Update () (at Assets/DestroyerScript.cs:27)

and this

 $$anonymous$$issingComponentException: There is no 'Camera' attached to the "Patch Pirate(Clone)" game object, but a script is trying to access it.
 You probably need to add a Camera to the game object "Patch Pirate(Clone)". Or your script needs to check if the component is attached before using it.
 UnityEngine.Camera.WorldToScreenPoint (Vector3 position)
 DestroyerScript.Update () (at Assets/DestroyerScript.cs:27)

and so on.

When I double click the in game exception errors, all of them lead to the 27th line in the destroyer script.

Any Ideas?

avatar image tanoshimi Ejpj123 · Apr 23, 2016 at 09:48 PM 0
Share

You probably need to add a Camera to the game object "Patch Pirate(Clone)". Well, do you?

avatar image Ejpj123 tanoshimi · Apr 29, 2016 at 02:42 AM 0
Share

It didn't work. When I ran the game, the screen just went blue, (from the camera I attached to the Patch Pirate). Should I put the camera attached to the Patch Pirate to the same position of the original camera? Or should I put the camera (Patch Pirate) in the same position of the Patch Pirate?

Show more comments
avatar image Ejpj123 · Apr 30, 2016 at 12:39 AM 0
Share

+Johat Yes, I want it so if I touch either side of the screen, it loads the next scene.

avatar image Ejpj123 Ejpj123 · Apr 30, 2016 at 12:41 AM 0
Share

It has also worked so far, to where it loads the scene when i touch the LEFT side of the screen. Thank you guys so much!

avatar image Johat Ejpj123 · Apr 30, 2016 at 01:19 AM 0
Share

Hi, glad to hear everything's working okay for you.

I updated the answer to also check the right side of the screen. It's no more complicated -- you just add in an extra check to see if the screen position is greater than the screeen width.

avatar image Ejpj123 Johat · May 01, 2016 at 12:33 AM 0
Share
 if (leftScreenPosition.x < 0f || rightScreenPosition > Screen.width)  ERROR = error CS0019: Operator `>' cannot be applied to operands of type `UnityEngine.Vector3' and `int'


It is on line 43

Do you know what the problem is?

This game is 2D by the way,

Show more comments
avatar image
0

Answer by Gaming-Dudester · Apr 19, 2016 at 01:33 AM

I don't fully understand but...try to make a gameobject that ALWAYS stays just outside the camera view. then use

    void OnTriggerEnter(Collision coll)
    {
      if (coll.gameobject.tag == "boundsgameobject") 
      {
          scenemanager.loadscene(gameover); 
      }
 } 

sorry if I misunderstood... I hope not tho. it would be better to show a pic of ur screen. is the game 2d or 3d? I don't fully understand what ur going for. hope thos helps.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ejpj123 · Apr 20, 2016 at 04:14 AM 0
Share

Result: alt text

screenshot-2016-04-19-222711.png (45.6 kB)
avatar image tanoshimi · Apr 20, 2016 at 05:51 AM 0
Share

Case matters - it's LoadScene(), which you can check easily in the API - http://docs.unity3d.com/ScriptReference/Scene$$anonymous$$anagement.Scene$$anonymous$$anager.LoadScene.html

avatar image Johat · Apr 20, 2016 at 06:24 AM 0
Share

Also, be careful: though fixing the function call from "loadscene" to LoadScene will remove compiler errors, this still won't work.

That's because this isn't the correct signature for OnTriggerEnter (check here).

If you switch parameter type from Collision to Collider you should be okay though =).

EDIT:

Just reading again -- also not sure if you're using 2D or 3D Colliders? It makes a difference!

If you're using 2D Colliders, you need OnTriggerEnter2D with a parameter of type Collider2D.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unwanted rotation 0 Answers

Collision2D error when instantiating a prefab. 1 Answer

How to tell if a 2D object is in a certain area 2 Answers

Limiting camera to visible scene 0 Answers

set Edge Collider 2D to the borders of the camera's viewport 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges