Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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
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
8

Answer by Navigatron · Jun 22, 2013 at 04:38 PM

Here ya go!
This is probably as simple as it can get!
Un-Parent the Camera from the Player, And attach this script to the camera.
Then simply drag you character into the script's target slot.

 var target : Transform;
 var distance : Float;
 function Update(){
 
     transform.position.z = target.position.z -distance;
     transform.position.y = target.position.y;
     transform.position.x = target.position.x;
 
 }

Have Fun!
-Navi

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 moviemastersdk · Jun 22, 2013 at 04:42 PM 0
Share

i suggest either having the z pos as a public float or just leaving it blank and allow the user to place the camera on the z axis manually, a fixed number makes it a bit hard to get the correct distance without any visual guidance.

avatar image Navigatron · Jun 22, 2013 at 04:48 PM 0
Share

Thanks for the Advice! Answer Edited.

avatar image FerdieQO · Oct 22, 2013 at 09:27 AM 3
Share

It didn't allow me to edit the properties of transform.position seperately so I needed to do it like this:

 var target : Transform;
 var distance : Float;
 function Update(){
  
     transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - distance);
  
 }
avatar image
4

Answer by unfoundfate · Apr 21, 2012 at 01:28 PM

Easiest way would be to unparent the camera. And throw something similar to the following in your update.

 int DistanceAway = 10;
 Vector3 PlayerPOS = GameObject.Find("Player").transform.transform.position;
 GameObject.Find("MainCamera").transform.position = new Vector3(PlayerPOS.x, PlayerPOS.y, PlayerPOS.z - DistanceAway);

This will keep the camera bound the the players X and Y coordinates while placing the camera 10 meters from the player. Then all you have to do is the the cameras rotation in the editor. That or for the rotation use .LookAt(target); with the player being the target.

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 Nepoxx · May 05, 2013 at 01:30 AM 2
Share

Please note that "Find()" is a very expensive operation.

avatar image Xenith · Dec 04, 2015 at 01:10 AM 0
Share

A really ancient necromancer reply, but seeing that I used this answer I'll add my two cents. As Nepoxx said, Find() is super expensive. I got around it by assigning the camera and player you find to a private class level variable at Start();

     public float cameraDistOffset = 10;
     private Camera mainCamera;
     private GameObject player;
 
     // Use this for initialization
     void Start () {
         mainCamera = GetComponent<Camera>();
         player = GameObject.Find("Player");
     }
     
     // Update is called once per frame
     void Update () {
         Vector3 playerInfo = player.transform.transform.position;
         mainCamera.transform.position = new Vector3(playerInfo.x, playerInfo.y, playerInfo.z - cameraDistOffset);
     }

avatar image
1

Answer by Fabkins · Apr 21, 2012 at 12:33 PM

Put this on the camera:

  function LateUpdate() 
  {  
  camera.transform.rotation=Quaternion.Euler(Vector3(90, 0, 0));  // 90 degress on the X axis - change appropriately
  }

It forces the rotation. I dont know if this is the best way of doing this but works.

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
1

Answer by Sanky · Dec 18, 2012 at 11:22 AM

var camTarget:Transform; function Start () {

}

function Update () {

transform.LookAt(camTarget);

}

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 Jaywalker · Oct 26, 2010 at 07:17 AM

Hey!

Take a look at the 2d platformer tutorial: here and check out the script on the camera

good luck

cheers!

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.

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