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 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.

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

Cinemachine 2d camera problem 0 Answers

Camera script 1 Answer

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

Smooth Camera 2D Follow Player 0 Answers

Camera dont move whit Smooth Follow 2D 0 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