Search Unity

Copy an objects rotation to a camera

Discussion in 'Scripting' started by Mertaan, Mar 29, 2013.

  1. Mertaan

    Mertaan

    Joined:
    Mar 29, 2013
    Posts:
    1
    Hello,
    I'm trying to follow a player with a camera.
    I can not just child it to the object due to prefab reasons.

    Right now, i have following of the object working, but i want it to rotate with the object as well.

    This is the part i'm having trouble with:
    Code (csharp):
    1.  
    2. void Update (){
    3. this.transform.localEulerAngles = this.transform.localEulerAngles + new Vector3(0,0, player.transform.rotation.y);
    4. }
    5.  
    It grabs the rotation value of the player correctly, but it spins the camera around with that value.
    I want it to put the same value in the camera's rotation instead.
     
  2. ardo314

    ardo314

    Joined:
    Jul 7, 2012
    Posts:
    345
    Code (csharp):
    1. void Update (){
    2.  
    3. this.transform.localRotation = Quaternion.Euler(new Vector3(0,player.transform.rotation.y,0));
    4.  
    5. }
    Something like this?
     
  3. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    this is happening because every frame your adding the players y euler angles to "this"s euler angles. basically say on the first frame your camera has a euler angle z value of 10, and the player has a rotation of on the y 20. the next frame the camera's z euler angle is 30, and the player is still at 20 y, it will keep doing this every frame so you just constantly spin around.... maybe try...
    Code (csharp):
    1. void Update (){
    2.     this.transform.localEulerAngles = new Vector3(this.transform.localEulerAngles.x, this.transform.localEulerAngles.y, player.transform.rotation.y);
    3. }