• 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 TysonG · Apr 02, 2019 at 03:08 AM · cameralerpsmoothtransition

How to make sliding room transitions like in Mega Man?

Hey everyone, so in Mega Man 2, when the player moves between rooms, the camera smoothly transitions to the view of the next room while the player freezes and moves ever so slightly into the next room (reference: https://www.youtube.com/watch?v=B5fyahaKCm4). I have the camera for the game already working, where there is a collider covering each room and if the player moves to another room the camera snaps to be viewing that room. I think there is something called Lerp that I could use but I wouldn't know how to use it. Any help would be appreciated!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class MegaManCamera : MonoBehaviour
 {
 
     [Header("Set in Inspector")]
     public Transform target;
     public GameObject camBoundsParent;
     [Tooltip("The distance from the center of the camera to the side of the frame.")]
     public float camHalfW = 8;
     [Tooltip("The distance from the center of the camera to the top or bottom of the frame.")]
     public float camHalfH = 7.5f;
     public float camOffsetZ = -10;
     [Tooltip("Must be between 0 & 1. Determines size of area in middle of screen that will not force scroll of camera.")]
     public float deadZoneSize = 0.5f; // What border area must the target enter to force a scroll of the camera 
 
     [Header("Set Dynamically")]
     public Collider currColld;
 
     private Collider[] camBounds;
 
 
     // Use this for initialization
     void Awake()
     {
         // Get all of the camBounds colliders
         camBounds = camBoundsParent.GetComponentsInChildren<Collider>();
         currColld = null;
     }
 
     void FixedUpdate()
     {
         // Get the position of the target
         Vector3 tPos = target.position;
 
         // Determine whether the target is no longer still within the same Collider
         if (currColld == null || !currColld.bounds.Contains(tPos))
         {
             // Find the Collider that the target IS within
             currColld = null;
             foreach (Collider colld in camBounds)
             {
                 if (colld.bounds.Contains(tPos))
                 {
                     // If we find a collider, then set currColld and break out of the foreach loop
                     currColld = colld;
                     break;
                 }
             }
         }
 
         // First, align the camera with the target (with a Z offset, of course)
         tPos.z += camOffsetZ;
 
         // Make sure that the Camera only moves if the target is outside the middle of the screen
         Vector3 cPos = transform.position;
         float deadZoneX = camHalfW * deadZoneSize;
         float deadZoneY = camHalfH * deadZoneSize;
         if (Mathf.Abs(tPos.x - cPos.x) > deadZoneX)
         {
             if (tPos.x > cPos.x)
             {
                 cPos.x = tPos.x - deadZoneX;
             }
             else
             {
                 cPos.x = tPos.x + deadZoneX;
             }
         }
         if (Mathf.Abs(tPos.y - cPos.y) > deadZoneY)
         {
             if (tPos.y > cPos.y)
             {
                 cPos.y = tPos.y - deadZoneY;
             }
             else
             {
                 cPos.y = tPos.y + deadZoneY;
             }
         }
 
         // if the currColld is not null, limit movement to within the Collider
         if (currColld != null)
         {
             // Account for camW and camH when judging the size of the bounds
             Vector3 min = currColld.bounds.min;
             Vector3 max = currColld.bounds.max;
 
             min.x += camHalfW;
             min.y += camHalfH;
             max.x -= camHalfW;
             max.y -= camHalfH;
 
             cPos.x = Mathf.Clamp(cPos.x, min.x, max.x);
             cPos.y = Mathf.Clamp(cPos.y, min.y, max.y);
         }
 
         transform.position = cPos;
     }
 }

Comment
Add comment · Show 2
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 toddisarockstar · Apr 02, 2019 at 05:02 AM 0
Share

you have not follow up on your previous questions or marked any correct. If users take the time to help you. you should take the time to follow up.

avatar image TysonG toddisarockstar · Apr 02, 2019 at 02:39 PM 0
Share

I did leave a comment on the other post of $$anonymous$$e that you responded to and I didn't accept the answer because it didn't work for my situation. Thanks for responding though.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by highpockets · Apr 02, 2019 at 06:20 PM

Lerp can do that indeed. A linear interpolation is exactly what you need:

 Vector3 camStartPos;
 Vector3 camEndPos;
 float lerpTime = 0.0f;
 bool triggerTransition = false;
 
 void LateUpdate(){
 if(triggerTransition){
 Camera.main.transform.position = Vector3.Lerp(camStartPos, camEndPos, lerpTime);
 if(lerpTime >= 1.0f){
 triggerTransition = false;
 }else{
 lerpTime = lerpTime + Time.deltaTime;
 }
 }
 }
 
 void OnTriggerEnter(Collider other){
 
 if(other.gameObject.tag == “Player”){
 triggerTransition = true;
 camStartPos = Camera.main.transform.position;
 lerpTime = 0.0f;
 }
 }
 

Didn’t test it, but should do the trick.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

164 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

Related Questions

Camera not slowly moving to object 0 Answers

How to smooth my camera (Vector3.Lerp) 0 Answers

Camera zoom smoothing 1 Answer

Problems with Lerp. 1 Answer

Smooth Camera/Object Movement 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