• 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 mheyman89 · Feb 26, 2014 at 07:33 AM · camera2dfollow

2D Camera follow player up but not down

Hey everyone.

I'm using the code from the Unity 2D game example tutorial for my camera movement. Right now it follow the character in any direction.

What I want is that if my character moves up in the Y direction, the camera follows it, but if it moves down in the Y, the camera doesn't follow. Basically I want the bottom of the camera to be the kill trigger, so the player should never fall. I've tried several things to no success, any thoughts?

 using UnityEngine;
 using System.Collections;
 
 public class CameraFollow : MonoBehaviour {
 
     public float xMargin = 0f;
     public float yMargin = 0f;
     public float xSmooth = 0f;
     public float ySmooth = 0f;
     public Vector2 maxXAndY;
     public Vector2 minXAndY;
 
     private Transform player;
 
     void Awake ()
     {
         player = GameObject.FindGameObjectWithTag("Player").transform;
     }
 
     bool CheckXMargin() 
     {
         return Mathf.Abs (transform.position.x - player.position.x) > xMargin;
     }
 
     bool CheckYMargin() 
     {
         return Mathf.Abs (transform.position.y - player.position.y) > yMargin;
     }
 
     void FixedUpdate ()
     {
         TrackPlayer();
     }
 
     void TrackPlayer ()
     {
 
         float targetX = transform.position.x;
         float targetY = transform.position.y;
 
         if(CheckXMargin())
             targetX = Mathf.Lerp (transform.position.x, player.position.x, xSmooth * Time.deltaTime);
 
         if(CheckYMargin())
             targetY = Mathf.Lerp (transform.position.y, player.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 · 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 Invertex · Feb 26, 2014 at 07:57 AM 0
Share

Try removing the $$anonymous$$athf.Abs, it converts values to always be positive, so you won't have any returned negative values for going down or backwards.

avatar image MrPotatoHQ · Oct 11, 2020 at 04:42 AM 0
Share

have you found any reliable solution to this? i have gone all over the internet and this is the only place that covers this topic.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Hakumaru · Feb 26, 2014 at 08:00 AM

try something along the line of, there's a trigger that has its position equal to the camera -5 or so on the y, which brings the "killswitch" up, then when setting camera to the players position, you make it only equal on the y axis if a variable is True.

if(!movingDown){

targetY = Mathf.Clamp (targetY, minXAndY.y, maxXAndY.y);

}

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 Hakumaru · Feb 26, 2014 at 07:52 AM 0
Share

That SHOULD work, i'm not positive since i cannot test it.

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

23 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

Related Questions

Shaky player effect when I apply a damping camera follow script? 1 Answer

Camera dont move whit Smooth Follow 2D 0 Answers

Smooth Camera 2D Follow Player 0 Answers

How to 2D Camera follow with lag. 1 Answer

Camera 2d following player with delay jumps 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges