Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Pixeltoro · Jun 11, 2016 at 06:58 PM · c#2d gamecamera follow

2d camera follow

Hey so to begin let me explain what the camera will need to do.

My game is in 2d using c#

In my game the player will be able to move the usual left and right but the main movement will be down. I want the camera to follow the player allowing for a limited amount of movement before it follows.

Essentially when player moves to the camera should stay until it reaches an invisible border allowing small movements before it follows.

The main effect if for when player is falling it will keep track of the player but if player slows his fall rate you should see the player move upwards slightly.

I have tried various tutorials online and none seem to be able to do what I need. I not sure if I am just wording it badly so I can't find the correct methods but I have got stuck

Help please

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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by 14ercooper · Oct 27, 2016 at 03:49 PM

A method of doing this is creating a CameraFollow script. The script should have a way to calculate distance to an object on each axis and a method to smoothly move to new coordinates (both have a lot of tutorials). Set the script up so that the camera is always a set number of units in the Z-plane from your player (assuming a standard 2d setup). When the distance from the camera in a direction hits a value (square root of the sum of the Z-offset squared and the distance before the camera moves squared) in the direction; call the smooth movement function repeatedly move to the player's location, offset by the Z-offset; until the distance from the camera to the player is the Z-offset.

If that doesn't make sense, let me know and I'll reword it / write a sample script

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

Answer by suicune2001 · Oct 28, 2016 at 07:51 AM

This is what I use for my 2D camera. I actually put an empty game object on my player and tag it "CameraTarget". (that way you can have the camera follow more above/below/to the side of the player as you want. If it doesn't have the tag then it won't work. You DO NOT child the camera to the player. You can play with the settings until you get something pretty close to what you want.

 using UnityEngine;
 using System.Collections;
 
 public class SmoothFollow : MonoBehaviour 
 {
     public float xMargin = 1.0f;
     public float yMargin = 1.0f;
 
     public float xSmooth = 10.0f;
     public float ySmooth = 10.0f;
 
     public Vector2 maxXAndY;
     public Vector2 minXAndY;
 
     public Transform cameraTarget;
 
     // Use this for initialization
     void Awake () 
     {
         cameraTarget = GameObject.FindGameObjectWithTag("CameraTarget").transform;
     }
 
     bool CheckXMargin()
     {
         return Mathf.Abs (transform.position.x - cameraTarget.position.x) > xMargin;
     }
 
     bool CheckYMargin()
     {
         return Mathf.Abs (transform.position.y - cameraTarget.position.y) > yMargin;
     }
 
     
     // Update is called once per frame
     void FixedUpdate () 
     {
         TrackPlayer();
     }
 
     void TrackPlayer()
     {
         float targetX = transform.position.x;
         float targetY = transform.position.y;
 
         if(CheckXMargin())
         {
             targetX = Mathf.Lerp (transform.position.x, cameraTarget.position.x, xSmooth * Time.deltaTime);
         }
 
         if(CheckYMargin())
         {
             targetY = Mathf.Lerp (transform.position.y, cameraTarget.position.y, ySmooth * Time.deltaTime);
         }
 
         targetX = Mathf.Clamp (targetX, minXAndY.x, maxXAndY.x);
         targetY = Mathf.Clamp (targetY, minXAndY.y, maxXAndY.y);
 
         transform.position = new Vector3(targetX, targetY, transform.position.z);
     }
 }
 
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
avatar image
0

Answer by Tajition · Oct 28, 2016 at 07:51 AM

https://www.youtube.com/watch?v=WL_PaUyRAXQ&list=PLFt_AvWsXl0f0hqURlhyIoAabKPgRsqjz∈dex=11

Take a look at this tutorial. I think this is pretty much what you want.

So summarize what he did, you basically create an area that the player can run around without moving the camera. But when the player reaches the edge of said area you start centering the camera on the player.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can I flip only my 'Player' gameobject, and l leave it's child object alone? 2 Answers

2D rigidbody top down movement problem 3 Answers

Will someone please help me??? 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