• 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 ArunChnadran · Apr 22, 2012 at 04:03 AM

I did something similar said by unfoundfate. The code that is attached to the camera goes like this:-

 var Stone : GameObject;
 
 function Update()
 {
     transform.position.y= Stone.transform.position.y; 
 // in this case I only needed to care about the height
 }

this was attached to the camera, now drag and drop the gameobject from Hierarchy(or prefab) to the Stone in Inspector panel. This worked!

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 Lukasz K. · Apr 12, 2013 at 11:46 PM 0
Share

This worked. Thanks allot... but I noticed the camera is droping to the bottom of the character (my character feet starts from 20.08 and my camera is set to 42 to look in the middle of it). is there any simple add to your code that will freeze the droop of the camera?

avatar image
0

Answer by RyanZimmerman87 · Apr 13, 2013 at 12:42 AM

Just put this script on your Main Camera. Set the cameraOrientationVector to how far from your character you want it to be. And tilt the camera in scene view for the angle.

  using UnityEngine;
         using System.Collections;
         
         public class CameraScript : MonoBehaviour 
         {
             Transform playerTransform;
             
             Vector3 cameraOrientationVector = new Vector3 (0, 15, -10f);
             
 
 void Start () 
     {
         
         
         
         playerTransform = GameObject.Find ("Player").transform;
     }
             
         void LateUpdate () 
             {
                 
                 transform.position = playerTransform.position + cameraOrientationVector;
                 
             }
         }

   
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 moviemastersdk · Jun 04, 2013 at 06:29 PM

     public Camera PlayerCamera;
     public GameObject Spawn;
 
 void Update() {
 Vector3 CamPos = new Vector3(player.transform.position.x,player.transform.position.y,0);
 camera.transform.position = CamPos;
 }

this code will "parent" the camera to the players x and y position, but allows you to control the Z pos and rotation yourself.

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 harveyhb · Nov 07, 2013 at 11:53 AM

Sorry to be a complete noob.

I've Un-parented the camera from the player, then created a new Js script and pasted the code Navigatron gave above then dropped the script onto the camera.

However, I get the following error. "Assets/Standard Assets/Character Controllers/Sources/Scripts/transform.js(5,16): BCE0018: The name 'Float' does not denote a valid type ('not found')."

And no target slot is created.

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 BobbyDoogle · Dec 08, 2013 at 03:47 AM

I just had great luck by implementing the following code in C#, follows player transform but not rotation. Note it requires the object to be followed to be tagged as "Player", still new to Unity and not sure how to reference directly:

public class CameraMovement : MonoBehaviour { private Transform player; private Vector3 relCameraPos;

 void Awake()
 {
     player = GameObject.FindGameObjectWithTag("Player").transform;
     relCameraPos = player.position-transform.position;
 }
 void Update()
 {
     transform.position = player.position-relCameraPos;
 }

}

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