Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 FrustratedRocka · Dec 04, 2014 at 07:49 AM · camera2d2d-platformerfollow

Advice on implementing metroidvania camera

I'm working on putting together a simple metroidvania to try and keep my programming skills... well, not sharp, but not completely rusty. I have a pretty good camera script that centers on the player as long as they aren't too close to the edge of the room.

It is, however, currently missing two crucial features that I have no idea how to implement. The first is that it needs the ability to move from room to room, preferably in a smooth animation that takes about 2 seconds.

The second is a bit more complex. I'd prefer it to stay slightly in front of the player so they can actually see where they're going, since in a platformer, that's kind of important. I tried setting up a node in front of the player and setting the camera to follow that, but that ended up causing problems with the boundaries and only working when the player was facing right. If possible, I'd prefer to handle this from within the camera script itself.

To elaborate on exactly what I'm going for, here's my understanding of the way Super Metroid's camera handles the X axis. The screen is 16 blocks wide, and assuming the camera is unconstrained by the walls, a running Samus ends up with 10 blocks in front of her and 6 blocks behind her. Additionally, when running towards the boundary of a room, the camera stops following Samus when she is 10 blocks from the edge. When running away from the boundary, the camera starts following her at 6 blocks from the edge. Should Samus change directions, it takes 5 blocks for the camera to travel the additional four blocks forward - in other words, it moves at a ratio of 1.8 to 1 until it reaches a maximum distance from Samus. I don't know of a way to measure movement along the Y axis, but I'd assume it works with similar numbers.

I'm working in C#, but if you know a solution that for some reason requires Javascript I shouldn't have any trouble switching over what I already have.

The code so far:

using UnityEngine; using System.Collections;

public class MetroCam : MonoBehaviour {

 public Transform target;
 public float smoothing = 8f;
 public Collider boundaries;
 public int screenHeight = 16;
 
 float roomY;
 float minX;
 float maxX;
 float minY;
 float maxY;
 int moveStart = 0;
 float smooth;

 Vector3 offset;
 Vector3 targetCamPos;

 void Start()
 {
     // Set up camera - options for both orthographic and 2.5D
     camera.orthographicSize = (screenHeight / 2);
     camera.fieldOfView = 20;
     targetCamPos.z = -6 * camera.orthographicSize;
     smooth = smoothing;
     offset = transform.position - target.position;

     Bounds roomBounds = boundaries.bounds;

     float vertExtent = screenHeight / 2;
     float horzExtent = vertExtent * Screen.width / Screen.height;
     roomY = roomBounds.size.y;

     minX = roomBounds.min.x + horzExtent;
     maxX = roomBounds.max.x - horzExtent;
     minY = roomBounds.min.y + vertExtent;
     maxY = roomBounds.max.y - vertExtent;
 }

 void FixedUpdate()
 {
     bool inXBounds = target.position.x >= minX && target.position.x <= maxX;
     bool inYBounds = target.position.y >= minY && target.position.y <= maxY;
     bool lockVert = roomY <= screenHeight;

     // X axis follow
     // TODO: Lock x axis for rooms that aren't as wide as the screen
     if(inXBounds)
     { 
         targetCamPos.x = target.position.x + offset.x;
     }

     // Y axis follow
     if(lockVert)
     {
         targetCamPos.y = screenHeight / 2 + offset.y;
     }
     else if(inYBounds)
     { 
         targetCamPos.y = target.position.y + offset.y;
     }

     // Force camera into starting position
     if(!lockVert && moveStart == 0)
     {
         smooth = Mathf.Infinity;
         targetCamPos.y = screenHeight / 2 + offset.y;
         moveStart = 1;
     }



     transform.position = Vector3.Lerp (transform.position, targetCamPos, smooth * Time.deltaTime);
     if(smooth != smoothing)
     {
         smooth = smoothing;
     }
 }

}

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 gotnull · Jun 18, 2015 at 03:14 PM

Have you tried this?

https://gist.github.com/Jellybit/9f6182c25bceac06db31

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

i want the camera stop follwing my player only on y axis when he jumps... 4 Answers

Camera scrolling? 1 Answer

2D Camera Smooth follow, FixedUpdate and LateUpdate odd difference, help needed. 1 Answer

Smooth Camera 2D 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 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