• 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 /
  • Help Room /
avatar image
0
Question by Alokdo · Oct 27, 2016 at 02:06 AM · camera2d2d game

2D Camera RPG

Hi, i tried to make a camera for a 2D game (an rpg) can follow the player and collide with the end of the map. I tried jus drag and drop the camera inside the player, put some blocks at the edges of the map and putting a Box Collider on the camera. This works, but very glitchy, because when the camera collides with the block it seems like its bouncing and the player still walking to the edge (i wanted it) but when the player walks to the opposite side doesn't come back to the center of the camera. Any ideas? (Sorry, i got a really bad english)

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

Answer by ElijahShadbolt · Oct 27, 2016 at 04:01 AM

Instead of parenting the camera to the Player GameObject, you could add a script that constantly updates the position of the camera to be directly over the player, but within the bounds of a minimum and maximum world position, like so:

RPG2DCamera.cs

 using UnityEngine;
 
 public class RPG2DCamera : MonoBehaviour
 {
     public Transform target; // set to the Player GameObject
 
     public Vector2 worldPosMin = new Vector2(-10f,-10f);
     public Vector2 worldPosMax = new Vector2(10f, 10f);
 
     void Update()
     {
         if (target != null)
         {
             Vector2 targetPos = target.transform.position;
 
             float x = Mathf.Clamp(targetPos.x, worldPosMin.x, worldPosMax.x);
             float y = Mathf.Clamp(targetPos.y, worldPosMin.y, worldPosMax.y);
 
             transform.position = new Vector3(x, y, transform.position.z);
         }
     }
 }

Just add this script to the Camera and remember to set the Player as its Target in the inspector.

Comment
Add comment · Show 1 · 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 Alokdo · Oct 31, 2016 at 12:50 AM 1
Share

Thanks! It works really well. But i like UnityScript and i won't change it for anything, so if somebody needs the code in js:

 #pragma strict
 
 var target : Transform; // set to the Player GameObject
 var worldPos$$anonymous$$in : Vector2 = new Vector2(-10f,-10f);
 var worldPos$$anonymous$$ax : Vector2 = new Vector2(10f, 10f);
 
 function Update () {
     if (target != null) {
         var targetPos : Vector2 = target.transform.position;
 
         var x : float = $$anonymous$$athf.Clamp(targetPos.x, worldPos$$anonymous$$in.x, worldPos$$anonymous$$ax.x);
         var y : float = $$anonymous$$athf.Clamp(targetPos.y, worldPos$$anonymous$$in.y, worldPos$$anonymous$$ax.y);
 
         transform.position = new Vector3(x, y, transform.position.z);
     }
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

131 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

Related Questions

2D box collider always detecting player,2D Box colliders detecting player when he is not there 1 Answer

2D movement with gravity shifting 0 Answers

CS1216 error 0 Answers

Music playing from 2 different scenes - how to fix? 1 Answer

Parallax moving objects X parallax with cameras 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