Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Camera not Following Inspector's Value (Transform) When Play

Discussion in 'Editor & General Support' started by mhoosey, Nov 28, 2014.

  1. mhoosey

    mhoosey

    Joined:
    Nov 28, 2014
    Posts:
    3
    Hi,
    Sorry for this, but I'm newbie. I swear.

    I was learning by following the "Project" provided by Unity. The "Roll-a-Ball". Was learning the camera. I followed the video, but with different value. Then, found a problem.

    So, with the inspector, I set the Position value with:



    Because I wanted the camera view be like this:



    But, when I tested it, I hit Play, the value changed by itself:



    And the camera position:



    The Main Camera is independent in the hierarchy.
    And the script for the camera:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CameraController : MonoBehaviour {
    5.  
    6.     public GameObject player;
    7.     private Vector3 offset;
    8.  
    9.     void Start () {
    10.  
    11.         offset = transform.position;
    12.    
    13.     }
    14.  
    15.     void Update () {
    16.  
    17.         transform.position = player.transform.position + offset;
    18.  
    19.     }
    20. }
    21.  
    So, how to use the value I set and not changing when I hit play?
    Thanks,
    I'm noob, hope you understand XD
     
  2. Ruekaka

    Ruekaka

    Joined:
    Sep 12, 2014
    Posts:
    119
    Not sure if I get the point but from what I understand your script will only work as expected if the Player has the initial position 0, 0, 0.
     
  3. mhoosey

    mhoosey

    Joined:
    Nov 28, 2014
    Posts:
    3
    So, I have to assign the value within the .cs file? Not in Unity?
     
  4. mhoosey

    mhoosey

    Joined:
    Nov 28, 2014
    Posts:
    3
    I thinnk i found the problem.

    Since i set the transform.position = player.transform.position + offset;

    so the Z position of the camera is + the Z position of the playerObject. So when I hit play, ofcourse it's getting far from the GameObject because the Z value of player.transform.position != 0 - it's about -8,3. So when entering the Play mode, the transform.position value of the camera isn't the way I set it. It became -18 ( -10 + (-8) = 18).

    Thanks anyway :D