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 /
  • Help Room /
avatar image
0
Question by Latheef · Sep 14, 2015 at 07:36 PM · unity 5camera follow

In Unity how do i make the camera follow a character in the y axis..?

i have a character which keeps moving in the y-axis and the camera needs to be fixed to follow with the character for some extent and when the character falls down camera should freeze at the point. The attached image will make u understand better-image

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
1
Best Answer

Answer by LiamSorta · Sep 16, 2015 at 12:17 PM

There's a few ways to go about doing this. If you want a really simple solution then you can add this script to your main camera:

Add this just above your Start() function:

 [SerializeField]
 GameObject player;

Then add this in your Update() function:

 transform.position = new Vector3(transform.position.x,
 GameObject.Find("yourcharacterobjectname").transform.position.y, 
 transform.position.z);

Then in the inspector drag your character onto the [None (GameObject] field in your script on the main camera.

If it was me I'd go with keeping the camera stationary and have the player jump as they need to with the background/obstacles generating downwards.

Comment
Add comment · Show 3 · 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 Latheef · Sep 16, 2015 at 01:06 PM 0
Share

i tried your script but it got Exception- "Object reference not set to instance of object".

avatar image Latheef · Sep 22, 2015 at 09:48 PM 0
Share

Finally got it to work..!! Thanks..!! What if i want to move the camera in the positive y axis only not in negative y axis..with the ball..?

avatar image LiamSorta Latheef · Sep 27, 2015 at 09:52 PM 0
Share

Sorry, been busy with moving into Uni and whatnot! So for this (if I'm understanding what you mean correctly) you could either use a clamp or just check the ball position.

$$anonymous$$g

 if(yourGameObject.transform.position.y > 0)
 {
     transform.position = new Vector3(transform.position.x, yourGameObject.y, -10);
 }
 else 
 {
     transform.position = new Vector3(transform.position.x, 0, -10
 }
 

Let me know if it works for you!

avatar image
1

Answer by Seyren · Sep 16, 2015 at 03:34 PM

If you want to follow it on a simple Axis, just attach a script to the camera where the GameObject is referenced.

Then just do in the update function:

    transform.position = new Vector3(transform.position.x,ball.transform.position.y, transform.position.z);
 
 

Being ball the object for example. Add ball as a public gameobject and reference it from the editor by dragging it from the scene and you should be good to go.

If you want to freeze at some point just add a bool to the script and done. So for example:

 private bool _freeze;
 public GameObject ball;
 
 void Update()
 {
     if (freeze)
        transform.position = new Vector3(transform.position.x, ball.transform.position.y, transform.position.z);
 }


You are basically using the same position for the camera on the Y axis, which is why you are using transform.x and transform.z

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 Cherno · Sep 15, 2015 at 10:40 PM 0
Share

This will not work; in fact, it' won't even compile and throw an error (in C#, that is). transform.position, eulerAngles and similar Vector3 variables always have to be assigned as a whole. Assigning single x, y, z values doesn't work.

avatar image Seyren Cherno · Sep 16, 2015 at 12:37 AM 0
Share

True, forgot to apply the Vector3, i'm going to adjust it.

avatar image
0

Answer by Cherno · Sep 14, 2015 at 11:11 PM

Easy. Just set the camera's transform.position.y to whatever you like after making it follow the player.

 public float min_y = -2f;
 
 void LateUpdate() {
       
       if(transform.position.y < min_y) {
            Vector3 pos = transform.position;
             pos.y = min.y;
             transform.position = pos;
      }
 }
Comment
Add comment · Show 7 · 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 Latheef · Sep 15, 2015 at 12:16 PM 0
Share

@Cherno - Thanks for the response..!! Sorry I'm new to coding and unity i added camera as a child to my character and your code to the camera. The camera moves with the character along the scene and when the character turns left the camera moves in the x axis with the ball which is what i dont want.The character is centered with the camera and hence exposing -x and x planes while turning left and right as in the attachment.. image .Could u elaborate in a detailed way please..!!

screenshot-622.png (18.6 kB)
avatar image Aviratex Latheef · Sep 15, 2015 at 04:07 PM 0
Share

I'm new to coding too but doesn't removing the camera from the character as a child fix the problem?

avatar image Latheef Aviratex · Sep 15, 2015 at 07:04 PM 0
Share

No it didn't...

Show more comments
avatar image Cherno · Sep 15, 2015 at 10:46 PM 0
Share

If your camera is a child of the player object, then following is handles automatically of course; The transformations that are applied due to movement and rotation of the parent object need to be overridden, which is a bit more complicated. I don't even know if it's possible, but you can try and put the code I posted above into the LateUpdate() function, which, as the name says, is called later and it might be called after parent-child transformations are applied:

     public float $$anonymous$$_y = -2f;
     public float camPos_x = 4f;
     
     void LateUpdate() {
           Vector3 pos = transform.position;
           if(pos.y < $$anonymous$$_y) {
                 pos.y = $$anonymous$$.y;
          }

         pos.x = camPos_x;
         transform.position = pos;
     }
 

If it doesn't work, unparent the camera and try this code:

  public Transform playerTransform;
  public float $$anonymous$$_y = -2f;

  void Update() {
       Vector3 pos = transform.position;

        pos.y = playerTransform.y;
        if(playerTransform.y < $$anonymous$$_y) {
              pos.y = $$anonymous$$.y;
       }
      transform.position = pos;
       
  }
avatar image Latheef Cherno · Sep 16, 2015 at 12:53 PM 0
Share

what do u mean by "$$anonymous$$.y" i'm getting errors....

avatar image Cherno Latheef · Sep 17, 2015 at 08:05 PM 0
Share

I meant $$anonymous$$_y, the variable that has been declared in the script's main body.

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

How to set Camera Limits in Unity that would stay accurate for different screen ratios? 2 Answers

Making camera follow the player the right way 0 Answers

So I'm attempting to make a camera at a fixed angle with no rotation,I'm trying to set my camera on a follow path while keeping a fixed angle. 1 Answer

Jittery Camera Follow Lerp 0 Answers

Camera yMin postion should be restricted to player postion 1 Answer


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