Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 EvanCheddar · Dec 18, 2019 at 11:52 PM · 2d gamecamera-movement

How can I get my camera to only follow my player down along the Y axis?

Hello Unity family,

Real quick. I'm making a 2D game where my player is moving downwards along the Y-axis. Nothing brings the player back up so I would like to lock my camera on the player. When my player jumps, the camera follows which is basically what i'm trying to prevent but I cant seem to find an "easy" solution for this. Thanks in advance if anyone can help me out!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Ruzihm · Dec 20, 2019 at 06:46 PM

Have the y component be the minimum of the current y position and the target's y position plus offset.

 public Transform target;

 public float Vector3 offset;

 void Update() 
 { 
     Vector3 targetPos = target.position;

     transform.position = new Vector3(
            targetPos.x + offset.x, 
            Mathf.Min(transform.position.y, targetPos.y + offset.y),
             targetPos.z + offset.z) ;
 }
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 EvanCheddar · Dec 20, 2019 at 07:03 PM 0
Share

This is the answer. Thank you.

avatar image
0

Answer by tashfiq103 · Dec 19, 2019 at 01:09 AM

using UnityEngine;

[RequireComponent(typeof(Camera))] public class CameraFollow : MonoBehaviour { #region Public Variables

 //Value : Default Target of the camera when the game started
 public Transform defaultTargetReference;
 public Vector3 cameraOffSet = Vector3.one;
 [Range(0f, 1f)]
 public float cameraFollowingVelocity = 0.1f;

 #endregion

 #region Private Variables

 private Transform m_TransformReference;
 //Value : The current target of the camera will always be stored in this variable, as we can change it through the runtime.
 private Transform m_CurrentTargetOfCamera;

 private Vector3 m_ModifiedPosition;

 #endregion

 #region Mono Behaviour

 private void Awake()
 {
     m_TransformReference = transform;

     //Initially, the current target of the camera is our default target which we set from the editor
     m_CurrentTargetOfCamera = defaultTargetReference;
 }

 private void LateUpdate()
 {
     //if : The current target of the camera is destroyed (Which you changed by calling the "ChangeCameraTarget()"), it will get back to its default target
     if (m_CurrentTargetOfCamera == null)
         m_CurrentTargetOfCamera = defaultTargetReference;

     //Calculating : Finding the new position of the camera considering the offset. for your case, I believe the offset would be (0,0,-10)

     //*************
     if (m_TransformReference.position.y > (m_CurrentTargetOfCamera.position.y + cameraOffSet.y))
     {

         m_ModifiedPosition = Vector3.Lerp(
                             m_TransformReference.position,
                             m_CurrentTargetOfCamera.position + cameraOffSet,
                             cameraFollowingVelocity
                         );

         //Assigning : the changed position
         m_TransformReference.position = m_ModifiedPosition;
     }

 }

 #endregion


 #region Public Callback

 /// <summary>
 /// Use this function to change the camera target in runtime.
 /// </summary>
 /// <param name="t_CameraTarget"></param>
 public void ChangeCameraTarget(Transform t_CameraTarget)
 {

     m_CurrentTargetOfCamera = t_CameraTarget;
 }

 #endregion

}

Comment
Add comment · Show 2 · 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 EvanCheddar · Dec 19, 2019 at 02:08 AM 0
Share

Hey thanks for the reply, I can't seem to get this code to work though.

avatar image tashfiq103 EvanCheddar · Dec 20, 2019 at 05:01 PM 0
Share

The code format not getting fixed. You can try this link : https://paste.ubuntu.com/p/NDNs5xrn96/ @EvanCheddar

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

156 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

Related Questions

camera script not working 1 Answer

How to apply fast & slow camera transition. 1 Answer

Cinemachine camera shake on button press 0 Answers

How to make the camera acquire the position of a gameobject and start following it? 1 Answer

Limiting camera to visible scene 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