• 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 Chobi77 · Sep 30, 2012 at 05:38 AM · c#camerafollowborder

Camera Border Follow

Hi all,

I am working on a orthographic camera and am trying to make it like the original Super Mario Brothers camera.

So far i have done the following:

Stopped the camera from moving up when Mario jumps, set the borders for the x axis so that the camera will stop scrolling when it reaches the left or right edge of the map and the camera also follows Mario around on the x axis left and right.

My problem is i don't want the camera to scroll to the left, what i need is for the edge border that i have set for the left hand side of the map to move forward when Mario moves to the right and then when Mario moves back to the left i need the camera to stop moving.

Here is my code thus far.

 using UnityEngine;
 using System.Collections;
 
 public class CameraScript : MonoBehaviour {
 
     //Object to look at and follow.
     public Transform target;
     
     //Border for width.
     public float borderX = 1.6F;
     
     //Border for height.
     public float borderY = 0.11F;
     
     //Stops camera from moving past the border to the left.
     public float minimum = 0.0F;
     
     //Stops camera from moving past the border to the right.
     public float maximum = 0.0F;
     
     //Time for camera dampen.
     float  smoothTime = 0.2F;
     
     //Mario's transform.
     private Transform marioTransform ;
     
     private Vector2 velocity;
     
     public bool useSmoothing = false;
     
     void Start()
     {
         marioTransform = transform;
     }
        
     void Update()
     {
        Vector2 newPos2D = Vector2.zero;
        
        if (useSmoothing)
        {
          newPos2D.x = Mathf.SmoothDamp(marioTransform.position.x, target.position.x + borderX, ref velocity.x, smoothTime);
        }
                
        Vector3 newPos = new Vector3(newPos2D.x, newPos2D.y, transform.position.z);
            
        transform.position = Vector3.Slerp(transform.position, newPos, Time.time);
         
        //Set the minimum and maximum vales for the camera movement on the x axis.
        transform.position = new Vector3(Mathf.Clamp(transform.position.x, minimum, maximum), transform.position.y, transform.position.z);
     }
     
     public void LateUpdate()
     {
      //Stop camera from moving in the y axis when mario jumps.
      Camera.main.transform.position = new Vector3(marioTransform.position.x, borderY, transform.position.z);
     }           
     
 }

The minimum value is for the border to the left and the maximum value is for the border to the right.

I need the minimum value to increment upwards when Mario moves to the right and when Mario moves to the left i need the incrementation to stop and the camera to remain where it is until Mario moves to the right again.

Thanks for reading and any help would be appreciated. :)

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 dabears · Oct 24, 2014 at 07:42 PM

Here is my camera script which does similar operations:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using UnityEngine;
 
 namespace namespaace{
     class EndlessLevelCameraController : MonoBehaviour {
         //public float speed = 2f;
         public float dampTime = 0.15f;
         private Vector3 velocity = Vector3.zero;
         public Transform rightEndMarker = null;
         public Transform leftEndMarker = null;
         public Transform topEndMarker = null;
         public Transform bottomEndMarker = null;
         public bool useFixedUpdate = false;
         public bool pinCamera = false;
 
         private float leftWallXWorld = 0;
         private float leftWallXScreen = 0;
         private float rightWallXWorld = 0;
         private float rightWallXScreen = 0;
         private float topWallYWorld = 0;
         private float topWallYScreen = 0;
         private float bottomWallYWorld = 0;
         private float bottomWallYScreen = 0;
 
         void Start() {
 
         }
         void updateWalls() {
 
             leftWallXWorld = leftEndMarker.position.x;
             leftWallXScreen = camera.WorldToScreenPoint(leftEndMarker.position).x;
 
             rightWallXWorld = rightEndMarker.position.x - Screen.width;
             rightWallXScreen = camera.WorldToScreenPoint(rightEndMarker.position).x - Screen.width;
 
             topWallYWorld = topEndMarker.position.y - Screen.height;
             topWallYScreen = camera.WorldToScreenPoint(topEndMarker.position).y - Screen.height;
 
             bottomWallYWorld = bottomEndMarker.position.y;
             bottomWallYScreen = camera.WorldToScreenPoint(bottomEndMarker.position).y;
         }
 
 
         void LateUpdate() {
             if (!useFixedUpdate)
                 updateCameraPosition();
         }
 
 
         void FixedUpdate() {
             if (useFixedUpdate)
                 updateCameraPosition();
         }
         // Update is called once per frame
         void updateCameraPosition() {
             updateWalls();
             if (PreferencesManager.CURRENT_PLAYER.transform) {
 
                 if (!pinCamera) {
                     Vector3 delta = PreferencesManager.CURRENT_PLAYER.transform.position - camera.ViewportToWorldPoint(new Vector3(0.5f, 0.5f, 0)); //(new Vector3(0.5, 0.5, point.z));
                     Vector3 destination = (transform.position + delta);
                     //Vector3 destinationWorld = camera.ScreenToWorldPoint(destination);
                     if (destination.x > rightWallXScreen) {
                         destination = new Vector3(rightWallXScreen, destination.y, 0);
                     }
                     if (destination.x < leftWallXScreen) {
                         destination = new Vector3(leftWallXScreen, destination.y, 0);
                     }
                     if (destination.y > topWallYScreen) {
                         destination = new Vector3(destination.x, topWallYScreen, 0);
                     }
                     if (destination.y < bottomWallYScreen) {
                         destination = new Vector3(destination.x, bottomWallYScreen, 0);
                     }
                     // here to always set the Z
                     destination = new Vector3(destination.x, destination.y, transform.position.z);
 
                     transform.position = Vector3.SmoothDamp(transform.position, destination, ref velocity, dampTime);
                 }
 
                 //Logger.logTest("step1");
                 float playerX = PreferencesManager.CURRENT_PLAYER.transform.position.x;
                 float playerY = PreferencesManager.CURRENT_PLAYER.transform.position.y;
                 if ((Math.Abs(playerX - leftEndMarker.position.x) < .5 && !PreferencesManager.getPlayerController().MOVING_RIGHT) ||
                     (Math.Abs(playerX - rightEndMarker.position.x) < .5 && PreferencesManager.getPlayerController().MOVING_RIGHT) ||
                     (Math.Abs(playerY - topEndMarker.position.y) < .6 && PreferencesManager.getPlayerController().MOVING_UP)) {
                     // || (Math.Abs(playerY - bottomEndMarker.position.x) < .5 && !PreferencesManager.getPlayerController().MOVING_UP)) {
 
                     PreferencesManager.getPlayerController().MOVE_ALLOWED = false;
                     //Logger.logTest("false");
                 } else {
                     PreferencesManager.getPlayerController().MOVE_ALLOWED = true;
                     //Logger.logTest("true");
                 }
 
             }
         }
     }
 }
 
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.

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

11 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

Related Questions

Distribute terrain in zones 3 Answers

Camera shake while following Player 1 Answer

How do I let a camera follow on one axis? 3 Answers

Rotating camera problem C# Android 2 Answers

2D Camera Follow boundaries 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