• 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
3
Question by LeviS · Oct 26, 2010 at 06:55 AM · camerarotationplayerparentchild

Need camera to follow player, but not the player's rotation.

So I'm building a simple platform game (think something like Mario, prince of persia, take your pick etc). The camera follows the player through the level, simply by being a child of the player's object.

The problem is that I made my character turn back and forth. I.E. if you are facing right and hit the left key, the character object is turned 180 degrees. That works great, except that the camera (being a child of the the Player) also turns 180 degrees!

Is there a way I can lock the camera's rotation, or just have it follow the player's movement on the X and Y axis, without taking the player's rotation?

I can think of some bizarre solutions that might work, but I'm betting there is a cleaner, more efficient solution then for example un-parenting the player from the camera before a flip, and then re-parenting the player to the camera afterward.

Thanks for all your help, having a blast with Unity :)

Comment
Add comment · Show 1
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 ArunChnadran · Apr 21, 2012 at 11:15 AM 0
Share

hi LeviS, got any improvement on this??!

13 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Joniz · Jan 28, 2014 at 10:46 PM

I just ran into this same problem and this is how I solved it…

I have a character and I have a CLOSEUP camera locked to it, facing the character. It activates and deactivates as I swap cameras around to different shots. This CLOSEUP camera is looking directly at the character to get a reaction shot and is perfect except when the character rotates. Then you get the weird GoPro-on-a-Flex-Clamp-facing-me effect, which I do not want. When I am getting a reaction shot and the character turns, I want the camera to remain steady.

I tried the scripts above but they became too cumbersome trying to figure out the right way to rotate & such as this character is moving and looking in all sorts of directions.

So, what I did was duplicate the parented camera (CAM 1) and unparented the dup (CAM 2). I then deactivated CAM 1 but left is attached to the parent so that it worked exactly as before. This camera (CAM 1) stays deactivated throughout the scene.

I then attached a script to CAM 2 so that it has a reference to CAM 1 and OnEnable (not Update), it updates it's position and rotation to match the reference CAM 2…

public class CameraLocs : MonoBehaviour {

 [SerializeField] private Transform cam1;
 
 
 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
     
 }
 
 void OnEnable() {
     
     if (target) {
         transform.position = cam1.position;
         transform.rotation = cam1.rotation;
     }
     
 }
 

}

Note that this is done OnEnable, so CAM 1 continues on it's swinging way but CAM 2 remains fixed in the location where it was when it turned on. It then is turned off, character moves, turned on, updates to face the character again. No problems.

Easy. And as far as I know, there's not much or any cost to having the extra deactivated camera hanging about. No worse than my trying to calculate the new position and rotation; and I assume the Unity people can do that a whole lot better than I can. (If I am wrong about this, please let me know. Thanks)

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 ARMagEDdonWATCHER · Apr 26, 2015 at 08:31 PM

This setup may be more complex, but you could create an anchor to follow the object's position, and then add a camera object as a child of the anchor with an offset.

For the anchor, you'd have the code:

 public Transform target;
 private Transform myTransform; //So we're not using GameObject.GetComponent() every frame
 
 void Update(){
      myTransform.position = target.position;
 }

You make the camera a child of the anchor, and then change its position and rotation in the Inspector window until you get the view you want.

This last part may be overkill, but you can additionally position empty game objects into the character as target transforms for the camera anchor to center on.

This setup uses more game objects, but it gives more control to the camera system. The anchor can act as a pivot that follows the target (and rotates, if you give it that functionality). It takes care of transformations in world space. The camera's local transform values won't be affected by the anchor's. This makes it easier to do cool things with the camera in local space, like panning or zooming via script, because you don't have to worry about where the camera is in world space. There probably are some limitations to this setup, but I'm not sure what they are.

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 VirtuallyHealthy · Jun 19, 2019 at 09:23 PM

 private GameObject player;
 private Camera mainCamera;
 private float zdistance = 3;
 private float ydistance = 5;

 void Start()
 {
     player = GameObject.FindGameObjectWithTag("Player");
     mainCamera = GetComponent<Camera>();
 }

 void LateUpdate()
 {
     mainCamera.transform.position = new Vector3(player.transform.position.x, (player.transform.position.y + ydistance), (player.transform.position.z - zdistance));
 }


P.S. it has to be late update or it spazzes out

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
  • ‹
  • 1
  • 2
  • 3

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Make a simple tree 1 Answer

How do I have a Child tell a Parent to move without the Child itself moving 0 Answers

First Person Camera not being child of the character/ Set child's rotation to global rotation 2 Answers

parent object rotate with child camera 1 Answer

Child affecting parent rigidbody? 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