- Home /
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 :)
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
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.
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);
}
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.
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);
}
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.
Answer by Sanky · Dec 18, 2012 at 11:22 AM
var camTarget:Transform; function Start () {
}
function Update () {
transform.LookAt(camTarget);
}
Your answer
Follow this Question
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