Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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
1
Question by Stoyanow · Jul 14, 2015 at 04:53 PM · camera2dcollisionstop

Stop camera when wall hit 2D game

Hello guys. I am trying to make the camera stop following the player, when it collides with a wall. Here's the script and an explanation after:

 using UnityEngine;
 using System.Collections;
 
 public class CameraControl : MonoBehaviour {
     
     public Player player;
 
     public bool isFollowing;
     
     public float xOffset;
     public float yOffset;
     
     
     void Start () {
         player = FindObjectOfType<Player> ();
         
         isFollowing = true;
         
         
     }
     
     
     void Update () {
         
         if (isFollowing)
             transform.position = new Vector3 (player.transform.position.x + xOffset, player.transform.position.y + yOffset, transform.position.z);
         
     }
 
     void OnTriggerEnter2D(Collider2D other)
     {
     
 
             isFollowing = false;
     }
 
     void OnTriggerExit2D(Collider2D other){
 
         isFollowing = true;
     }
 }

  • Attach Box Collider2D and a Rigidbody2d to the camera.

  • Made 2 box colliders on the wall - 1 trigger, 1 not.

the screen starts shaking on collision. I know it is because I should use velocity or addforce in order to move the camera, since the transform.translate teleports the object. However I am really comfortable with this script. So I want to ask you, awesome guys, if there is a way, to stop the camera when it collides (hits) the wall in a room (for example), so that the player moves freely to the edge of the screen, but the camera stays in one place, until he returns to the middle of the screen, then the camera follows again.

Any help is greatly appriciated!

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
2
Best Answer

Answer by lloladin · Jul 14, 2015 at 09:31 PM

i think its somethign like this you need

     // player
     public Transform playerToFollow;
     
     // walls
     public Transform Firstwall;
     public Transform Lastwall;
 
     // camera rules
     public float Height;
     public float Zoom;
     public float max;
     public float min;
     
     void Start () {
         min = Firstwall.position + 2f;
         max = Lastwall.position - 2f;
     }
     
     void Update () {
 
         // if players position > min and player position < max
         if (playerToFollow.position.x > min && playerToFollow.position.x < max)
         {
             // change camera's position = players position.x height.y zoom.z
             transform.position = new Vector3 (playerToFollow.position.x, Height, Zoom);                
         }
     }

in this script i simple Check in my Update if the Players Position is bigger than min and smaller than max if both conditions are met the camera will follow the player if the conditions are not met the camera will stop following the player.

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
avatar image
1

Answer by Stoyanow · Jul 15, 2015 at 09:42 AM

Thanks lloladin for the answer. Simple and easy, and smart! I'm suprised, currently, there isn't anything on the web about this simple issue. A lot of games have this feature, and yet, not in the forums or here, there was a simple answer to that particular question.

Here is my finished script, there were some simple mistakes in the upper one:

 using UnityEngine;
 using System.Collections;
 
 public class CameraControl : MonoBehaviour {
     
     public Player player;
 
     public float xOffset;
     public float yOffset;
 
     public Transform firstWall;
     public Transform lastWall;
 
     public float Height;
     public float Zoom;
     public float max;
     public float min;
     
     void Start () {
         player = FindObjectOfType<Player> ();
 
         min = firstWall.transform.position.x + 2f;
         max = lastWall.transform.position.x - 2f;
     }
     
     
     void Update () {
 
         if (player.transform.position.x > min && player.transform.position.x < max)
         {
             // change camera's position = players position.x height.y zoom.z
             transform.position = new Vector3 (player.transform.position.x + xOffset, player.transform.position.y + yOffset, Zoom); 
         
         }
 
     }
 
 }
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

22 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

Related Questions

Modular Room System Similar to Mega Man 0 Answers

Simple movement problem. Need units to stop dead on collision. 3 Answers

2D Camera Collision 1 Answer

Display 1 No cameras rendering when player collides with enemy 1 Answer

2D Platformer Demo Camera Follow script doesn't follow player 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