Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 everoell · Nov 18, 2021 at 04:24 AM · 2d2d gametopdowntop-down

How to have camera follow player unless it hits a boundary

I'm currently making a top-down 2D game. The way I want the player and camera movement is much like the game undertale. My player can move freely within a set of boundaries. I want my camera to follow the player and keep the player centered to it. But if the camera hits the boundaries and stops moving, I want the player to be able to keep moving up until the boundaries as well even if it means leaving the middle of the camera. I then want the camera to go back to following the player in the center if it's not hitting any boundaries anymore.

the code I used to follow the player and stay centered was this -

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class CameraControls : MonoBehaviour { public GameObject player; private Vector3 offset = new Vector3(0, 0, -10);

 // Start is called before the first frame update
 void Start()
 {

 }

 // Update is called once per frame
 void LateUpdate()
 {
     transform.position = player.transform.position + offset;
 }

}

this did a pretty good job, but unfortunately, it neglects the boundaries I have set for the camera in another script in favor of following the player and keeping the player centered in the camera view.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by KloverGames · Nov 18, 2021 at 01:28 PM

You can detect collisions by using OnCollisionEnter2D(Collision col).

Make sure you have a boolean to tell when you crossed the boundary

When you cross the boundary, set the cameraFollow variable to false.

As you can see, I've added an if statement in the LateUpdate() function for you that says whenever cameraFollow is true, the camera will follow the player's position.

So in OnCollisionEnter2D(), we set it to false because the player passed a boundary.

 bool cameraFollow;

 void OnCollisionEnter2D(Collision col)
 {
     if(col.gameObject.tag == "Boundary")
     {
         cameraFollow = false;
     }
 }


 void LateUpdate()
 {
     if (cameraFollow)
     {
         transform.position = player.transform.position + offset;
     }
 }
Comment
Add comment · Show 4 · 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 everoell · Nov 18, 2021 at 07:25 PM 0
Share

Thank you so much! with a little tweaking this seems to work very well.

I have 2 follow-up questions. You have the boundary as an object with a tag, my boundaries were made with coding and the axes. What object do you suggest I make the boundaries with?

My second question, Unity is giving me an error on the OnCollisionEnter2D. Script error: OnCollisionEnter2D This message parameter has to be of type: Collision2D

avatar image KloverGames everoell · Nov 18, 2021 at 07:29 PM 0
Share

You can make the boundary a cube and scale it bigger or smaller. Just disable the MeshRenderer component in the hierarchy afterwards.

For the second question, I’m not sure if you are familiar with parameters but a parameter is what’s inside of the parenthesis

void OnCollisionEnter2D(parameter variableName)

So change the parameter like this

void OnCollisionEnter2D(Collision2D col)

avatar image KloverGames · Nov 18, 2021 at 07:30 PM 0
Share

Let me know if what I said was confusing, I will explain it easier.

avatar image everoell KloverGames · Nov 19, 2021 at 12:52 AM 0
Share

I understand it, thank you

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

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

Related Questions

8-directional orientation, top down 2D, seperate from movement 0 Answers

Prefab to a tile script 0 Answers

how to make a pushable object in a 2D top-down game? (no gravity) 1 Answer

Jump functionality for a 2D Top Down game? 1 Answer

Simple top-down 2D collision 2 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