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 laurdtkz · Jan 02, 2019 at 07:27 PM · collision detectioncamera controlscamera script

How could i make a collision happen based on camera view ?

I'm a beginner and I'm trying to make a game and i need help with collision. How could i make a collision happen based on camera view ,like if the player hits the edge of camera to destroy the player. The camera doesn't move ,I tried adding box collider but if I change game resolution camera gets bigger or smaller based on resolution and the box collider it's not at the edge anymore

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by JxWolfe · Jan 02, 2019 at 07:33 PM

What if you raycast from the edges of the screen, and used those points to scale a collider so it fits perfectly inside the camera, no matter what the screen size is, then use OnTriggerExit to destroy the player. -Your collider needs to be a trigger for that

Code:

 void Start()
     {
         RaycastHit hit1;
         Ray ray1 = Camera.main.ScreenPointToRay(new Vector3(Screen.width,Screen.height,0));
 
         RaycastHit hit2;
         Ray ray2 = Camera.main.ScreenPointToRay(new Vector3(0,0,0));
         
         if (Physics.Raycast(ray1, out hit1)&&Physics.Raycast(ray2, out hit2)){//},100,LayerMask.NameToLayer("Ground"))) {
             Debug.Log("Hit1 "+hit1.point.ToString());
             Debug.Log("Hit2 "+hit2.point.ToString());
 
             transform.position = new Vector3((hit1.point.x-hit2.point.x)/2+hit2.point.x,(hit1.point.y-hit2.point.y)/2+hit2.point.y,transform.position.z);
 
             transform.localScale = new Vector3(hit1.point.x-hit2.point.x,hit1.point.y-hit2.point.y,transform.localScale.z);
         }
         else{
              Debug.Log("Miss");
         }
     }

Requirements: (for you)

*must have a box collider that is a trigger

*box collider must be larger than anything the screenSize might cover (MUCH LARGER)

*No mesh renderer needed

*if Camera.main != wanted camera, change Camera.main to a public Camera variable

*Script MUST be on box collider

*Will be effected if under a parent with a SCALE different from (1,1,1)

Enjoy! Tell me if you encounter issues, however, it worked fine on my computer.

Comment
Add comment · Show 10 · 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 JxWolfe · Jan 02, 2019 at 07:33 PM 0
Share

tell me if you need code

avatar image laurdtkz JxWolfe · Jan 02, 2019 at 07:44 PM 0
Share

well i could use the help

avatar image Snipe76 · Jan 02, 2019 at 07:35 PM 0
Share

I see we are both everywhere huh? :)

avatar image JxWolfe Snipe76 · Jan 02, 2019 at 09:06 PM 0
Share

yep, but you are going to keep getting the 'Best Answer'

avatar image JojoIV · Jan 02, 2019 at 07:44 PM 0
Share

If it helps here is an extension method i use to get the rectangle of an orthographic camera in world space:

 public static Rect GetRect (this Camera camera) {
     Vector2 cameraExtents = new Vector2 (camera.aspect * camera.orthographicSize, camera.orthographicSize);
     return new Rect ((Vector2) camera.transform.position - cameraExtents, cameraExtents * 2);
 }
avatar image JxWolfe · Jan 02, 2019 at 08:36 PM 0
Share

oh, it doesn't contain code to destroy the player, use --- void OnTriggerExit() --- for that

avatar image JxWolfe · Jan 02, 2019 at 08:42 PM 0
Share

note, it does need to be positioned FLAT on to the camera, otherwise you encounter some issues around the edges of the camera's view... however normally BOTH ortho AND perspective work...

Show more comments
avatar image
1

Answer by Snipe76 · Jan 02, 2019 at 07:34 PM

You need to access the renderer component in your gameObject that has a mesh on it. the renderer has a built-in bool that is true or false based on visibility by cameras. Here is an example in one of my scripts:

     private void CheckVisibleByCamera()
     {
         if (Camera.main)
         {
             if (VisabilityParts[0].GetComponent<Renderer>().isVisible)
             {
                 isVisibleByCamera = true;
             }
             else
             {
                 isVisibleByCamera = false;
             }
         }
     } //Check if player is visible by the camera.

Note: isVisible will be true if other cameras in the scene see that mesh.

or alternatively you can get the resolution of the camera you are using and convert it to world position and place the box colliders on the edges.

          Vector3 pos = Camera.main.WorldToViewportPoint (transform.position);
          pos.x = Mathf.Clamp01(pos.x);
          pos.y = Mathf.Clamp01(pos.y);
          transform.position = Camera.main.ViewportToWorldPoint(pos);

Comment
Add comment · 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

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

101 People are following this question.

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

Related Questions

Camera Controller movement issue 0 Answers

Making a camera switch in JS, how do I add more cameras? 1 Answer

Disable camera rendering outside of trigger 0 Answers

Making Camera Follow Object in only 1 direction 2D 1 Answer

3d ground for 2d gameplay / side scroller 1 Answer


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