x


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 :)

more ▼

asked Oct 26 '10 at 06:55 AM

LeviS gravatar image

LeviS
27 5 5 6

hi LeviS, got any improvement on this??!

Apr 21 '12 at 11:15 AM ArunChnadran
(comments are locked)
10|3000 characters needed characters left

6 answers: sort voted first

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.

more ▼

answered Apr 21 '12 at 01:28 PM

unfoundfate gravatar image

unfoundfate
81 1

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

May 05 at 01:30 AM Nepoxx
(comments are locked)
10|3000 characters needed characters left

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;

            }
        }
more ▼

answered Apr 13 at 12:42 AM

RyanZimmerman87 gravatar image

RyanZimmerman87
373 1 2 4

(comments are locked)
10|3000 characters needed characters left

Hey!

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

good luck

cheers!

more ▼

answered Oct 26 '10 at 07:17 AM

Jaywalker gravatar image

Jaywalker
514 15 21 30

(comments are locked)
10|3000 characters needed characters left

var camTarget:Transform; function Start () {

}

function Update () {

transform.LookAt(camTarget);

}

more ▼

answered Dec 18 '12 at 11:22 AM

Sanky gravatar image

Sanky
151 1 5

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Apr 21 '12 at 12:33 PM

Fabkins gravatar image

Fabkins
675 15 20 26

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3012
x2168
x807
x426
x413

asked: Oct 26 '10 at 06:55 AM

Seen: 9433 times

Last Updated: May 05 at 01:30 AM