Search Unity

Direction calculation of camera

Discussion in 'Scripting' started by gudula, Oct 9, 2015.

  1. gudula

    gudula

    Joined:
    May 20, 2015
    Posts:
    125
    Vector3 fwd = highlighted.transform.TransformDirection(Vector3.forward);

    How do you change this if you camera angle changes also. How do I make forward follow the camera?
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Why not just use transform.forward?
     
  3. gudula

    gudula

    Joined:
    May 20, 2015
    Posts:
    125
    at the moment Im using back for the angle Im on. I want to calculate which angle my camera is so I can move something towards it. What is it towards camera?
     
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Camera forward you can just use camera.transform.forward
    From object to camera is camera.transform.position - object.transform.position (then normalize if needed)
     
  5. gudula

    gudula

    Joined:
    May 20, 2015
    Posts:
    125
    I would like to calculate the direction to the camera and the direction away from camera.
    Vector3 fwd = highlighted.transform.TransformDirection(camera.transform.position);
    This can work to get towards camera but what is wrong with
    camera.transform.back?
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Time for a picture. I have no idea what you are talking about.

    As a general rule vector math always starts with a picture. I keep a pad right next to my computer for just that reason.
     
  7. gudula

    gudula

    Joined:
    May 20, 2015
    Posts:
    125
    Ok imagine you got 4 objects infront of you. You select the one in the middle and then you use your left arrow key to move the highlighted selected object to highlight the object left of the selected object with Vector3.left
    Now imagine I fly around to the other side of the screen and look at objects on the other side of it. How do I automatically adjust Vector3.left to go left instead of right?
    What is the routine here? You created Vector up,left,down,forward and all that but it is not tied to the camera or is it?
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    My imagination sucks. You got paint handy?
     
  9. gudula

    gudula

    Joined:
    May 20, 2015
    Posts:
    125
    I got a 3d puzzle that Im trying to switch peices with arrow keys but it works on default camera view. I dunno what todo with vector3.down and left and that when I switch to the other sides. Default view is ok. You can go vector3.left and it goes left but on the other side it goes right.