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 positioning issues

Discussion in 'Scripting' started by zerotag, Mar 11, 2016.

  1. zerotag

    zerotag

    Joined:
    Jun 20, 2015
    Posts:
    11
    Hello,

    I'm creating a lot of spheres in my scene on x and y axis, z is always 0.
    So I want to position my camera so that I look from above, down to my spheres.
    How do I do that?

    What I've tried so far?
    I'm taking the first sphere and took their coordinates and changed the z Value and then called LookAt with the sphere coordinates.
    But it looks like that I'm seeing my sphere's from the right side!?
    I also tried to set the first sphere as a parent to my camera and then called LookAt, but it looks like nothing has changed.

    The camera is nothing special at the moment, just the normal one.

    So any help would be appreciated.
     
  2. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    I see no reason why that wouldn't work. Could you post some code?
     
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    y is the vertical axis, not z...
     
    zerotag and ericbegue like this.
  4. zerotag

    zerotag

    Joined:
    Jun 20, 2015
    Posts:
    11
    Good point. Thank's!

    My old code looks like this:
    Code (CSharp):
    1.  
    2. Vector3 starPos = new Vector3(doubleToFloat(x), doubleToFloat(y), 0);
    3.  
    after I changed it to:
    Code (CSharp):
    1.  
    2. Vector3 starPos = new Vector3(doubleToFloat(x), 0, doubleToFloat(y));
    3.  
    And my camera code to

    Code (CSharp):
    1.  
    2. GameObject camera = GameObject.Find("Camera");
    3. Vector3 starPosition = stars[0].transform.position;
    4. Vector3 cameraPosition = starPosition + new Vector3(0, 30, 0);
    5. camera.transform.position = cameraPosition;
    6. camera.transform.LookAt(starPosition);
    7.  
    Everything looks as expected. :)
     
    ericbegue likes this.
  5. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    If you have only one camera in your scene, it would be faster to do:
    Code (CSharp):
    1.  GameObject cameraGO = Camera.main.gameObject
     
  6. zerotag

    zerotag

    Joined:
    Jun 20, 2015
    Posts:
    11
    I found similar code too, but it gives me a NullReferenceException, because
    Code (CSharp):
    1. Camera.main
    is null.
     
  7. ericbegue

    ericbegue

    Joined:
    May 31, 2013
    Posts:
    1,353
    It's probably because the camera is not tagged with "MainCamera".
     
  8. zerotag

    zerotag

    Joined:
    Jun 20, 2015
    Posts:
    11
    Tagged it and now it works. :)
     
    ericbegue likes this.