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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Camera based direction

Discussion in 'Scripting' started by yanuaris, Feb 25, 2016.

  1. yanuaris

    yanuaris

    Joined:
    Oct 16, 2015
    Posts:
    61
    Trying to script movements that is based on the camera...
    Basically wherever the camera is pointing, the directions are relative to it.

    Currently my direction is still based on the unit's own transform, and is independent from the camera's view.
    I have mouse orbit based camera script so the camera is already locked on.

    1. There'll be two camera so i probably need a way to right away reference camera_player_1, if play as player 1 and computer will do the same too for future gameplay.
    2. I'm wondering also how to be independent from the get inputaxis script?
    Like, if i want to use WASD and customizable directional button, how do i do that?

    Code (CSharp):
    1.  
    2.     public void FixedUpdate ()
    3.     {
    4.         float h = Input.GetAxis ("Horizontal");
    5.         float v = Input.GetAxis ("Vertical");
    6.         //bool j = Input.GetButton("Jump");
    7.         movto (h,v);
    8.         //gravity (j);
    9.         //stepto (q);
    10.     }
    11.  
    12.     void movto(float h, float v)
    13.     {
    14.         Vector3 direct = new Vector3 (h, 0.0f, v);
    15.         Vector3 todirect = post.position + direct;
    16.         post.LookAt (todirect, post.up);
    17.      
    18.         float tspd = spd + (h * v);
    19.         Vector3 mov = direct.normalized * tspd * Time.deltaTime;
    20.         body.MovePosition (post.position + mov);
    21.  
    22.     }
    This currently the bulk that process most of the gameplay...
     
  2. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    lets see if we cant learn something...

    Code (csharp):
    1.  
    2.         // first, lets get the camera's transform
    3.         Transform cam = Camera.main.transform;
    4.        
    5.         // now, lets get a rotation based off the camera's forward direction, but lets keep the y at zero.
    6.         // we use LookRotation so that we create a rotation based off of a vector
    7.         // if we supplied another vector to this, it would be the up direction.
    8.         Quaternion rot = Quaternion.LookRotation(new Vector3(cam.forward.x, 0, cam.forward.z));
    9.  
    10.         // now, lets use it as we need.
    11.         // (rot * Vector3.forward) gives us a direction based off of the quaternion's direction.
    12.         // if we used (rot * move) it would give us the direction based off the quaternion
    13.         // for a movement.
    14.         Vector3 endPoint = cam.position + (rot * Vector3.forward) * 5;
    15.  
    16.         // now lets draw a line to the end point.
    17.         Debug.DrawLine(cam.position, endPoint, Color.red);
    18.  
    I always did this the long way.. this seems pretty easy though.
     
  3. yanuaris

    yanuaris

    Joined:
    Oct 16, 2015
    Posts:
    61
    Interesting....

    Debugging line to test the feature first. Now that's new for me.
    Thank you!

    One question though...
    i've created beforehand tags and properties i'll be using so my camera is basically CameraPlayer, and CameraAI?

    How can i reference those camera then?
     
  4. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    The player should be using the main camera... ( the camera tagged as MainCamera). I do not see a point that the ai should have a camera though.

    Consider that the AI needs to move towards the player. That would be something like this:

    Code (csharp):
    1.  
    2. Vector3 move = player.transform.position - transform.position;
    3. move.y = 0;
    4. move.Nomalize();
    5.  
    6. // now we have a XZ vector which we can move thorugh space.
    7.  
    8. controller.SimpleMove(move * speed);
    9.  
     
    yanuaris likes this.
  5. yanuaris

    yanuaris

    Joined:
    Oct 16, 2015
    Posts:
    61
    The AI needs a camera because the game will have spectating features, so instead of an "AI camera"
    its player 2 camera. Maybe it's just additionals, i suppose.

    From that camera will also be the point of reference when the AI needs to consider the battlefield and make intelligent move around the field.
     
  6. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    In this case, I wouldn't use a camera at all.. I would use an empty game object, and when you want to spectate, I would move the main camera to the spectator camera position. (in the update) I am highly against creating more cameras in the scene than you actually need.