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

Isometric Follow Camera which moves to Position through key inputs.

Discussion in 'Scripting' started by Keeves, Aug 11, 2014.

  1. Keeves

    Keeves

    Joined:
    May 19, 2013
    Posts:
    3
    Hello.
    I've been working on this problem for most of the day scouring the internets for a solution to this problem with little luck. Hopefully the fantastic people here will be able to help :)

    I'll start with my script I have now for the Camera to follow a GameObject.

    Code (CSharp):
    1. public class CameraFollow : MonoBehaviour
    2.  
    3. {
    4.     public GameObject target = null;
    5.    
    6.     private Vector3 positionOffset = Vector3.zero;
    7.  
    8.     void Start ()
    9.  
    10.     {
    11.        
    12.         positionOffset = transform.position - target.transform.position;
    13.     }
    14.    
    15.  
    16.     void Update ()
    17.  
    18.     {
    19.  
    20.         if (target != null)
    21.         {
    22.             transform.LookAt (target.transform);
    23.  
    24.             transform.position = target.transform.position + positionOffset;
    25.         }
    26.  
    27.     }
    28. }
    Essentially what I want to do is to be able to hit a key (say 'Q' for example) and the camera moves along to a position 90 degrees to the left and stays there till I hit 'Q' again where it moves another 90 degrees. If I were to hit 'Q' four times consecutively it would bring the camera to its original position. Same thing if I were to hit 'E' it would bring the camera to the right and so on.

    I hope I'm explaining it right.
    Please if someone has a solution that would be great, thanks!
     
  2. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    Make the camera a child of the object and then use this method.

    1. Set a variable that you will store the amount of degrees to rotate (should be a private float, I called it 'targetAngle').
    2. Create two Ifs (one for Q one for E). Q should be a negative value, and E a positive value. Set the float variable to -=, += this value respectively.
    3. Tell the program that if your float variable is not equal to '0' than Rotate()
    4. Create the Rotate() method and give it two ifs as well.
    5. The first if deals with the float being >0 the second <0. For both use "transform.RotateAround(transform.pos point in space, Vector3.up axis in space because you want to rotate around the y-axis in isometric, angle in which you want it to rotate I created another var for this called 'rotationAmount')
    6. Under this argument for both if statements you then tell it to -= or +=, depending on which direction you are rotating, to the private float you created at the beginning.
    Hope that helps.
     
  3. Keeves

    Keeves

    Joined:
    May 19, 2013
    Posts:
    3
    It helps loads, thanks. Obviously I'm new to coding so a visual of the code with some notes will be wonderful, otherwise I'll just head down and power through this explanation till I figure it out.
    Thanks
     
  4. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    If you can't figure it out then I'll help code some of it but try to look up the API with the commands I've showed you.
     
    NomadKing likes this.
  5. Keeves

    Keeves

    Joined:
    May 19, 2013
    Posts:
    3
    Well, been knocking on the code for some time. Took a long time to get the code working without any errors, heh. So far, this is what I have and it works until I hit Q or E then the camera just goes a little crazy.
    Here is what I got:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class RotationalCamera : MonoBehaviour {
    5.  
    6.     private float targetAngle;
    7.  
    8.     void Start ()
    9.     {
    10.    
    11.     }
    12.  
    13.     void Update ()
    14.     {
    15.         if (Input.GetKey(KeyCode.Q))
    16.             targetAngle -= 1 ;
    17.         if (Input.GetKey(KeyCode.E))
    18.             targetAngle += 1 ;
    19.         if (targetAngle != 0)
    20.             Rotate() ;  
    21.     }
    22.  
    23.     private float Rotate ()
    24.     {
    25.         if (targetAngle > 0)
    26.             transform.RotateAround(transform.position, Vector3.up, +90);
    27.         if (targetAngle < 0)
    28.             transform.RotateAround(transform.position, Vector3.up, -90);
    29.         return 0;
    30.     }
    31. }
     
  6. Troas

    Troas

    Joined:
    Jan 26, 2013
    Posts:
    157
    Keep the target angle to 90, I'll post up my code in a few seconds here.


    Code (CSharp):
    1. void Update()
    2. public GameObject targetObject;
    3. private Vector3 playerPos;
    4. private float targetAngle = 0;
    5. const float rotationAmount = 1.5f;
    6. public float rDistance = 1.0f;
    7. public float rSpeed = 1.0f;
    8.  
    9.  
    10.    void Update() {
    11.  
    12.         playerPos.x = targetObject.transform.position.x;
    13.         playerPos.y = targetObject.transform.position.y;
    14.    
    15.         // Trigger functions if Rotate is requested
    16.         if (Input.GetKeyDown(KeyCode.Q)) {
    17.             targetAngle -= 90.0f;
    18.         }
    19.         if (Input.GetKeyDown(KeyCode.E)) {
    20.             targetAngle += 90.0f;
    21.         }
    22.    
    23.  
    24.         if(targetAngle !=0)
    25.         {
    26.             Rotate();
    27.         }
    28.     }
    29.  
    30.     protected void Rotate()
    31.     {
    32.    
    33.         float step = rSpeed * Time.deltaTime;
    34.         float orbitCircumfrance = 2F * rDistance * Mathf.PI; // Makes the camera orbit based on circle
    35.         float distanceDegrees = (rSpeed / orbitCircumfrance) * 360; // Distance
    36.         float distanceRadians = (rSpeed / orbitCircumfrance) * 2 * Mathf.PI; // Arclength
    37.    
    38.         if (targetAngle>0)
    39.         {
    40.             transform.RotateAround(playerPos, Vector3.up, -rotationAmount);
    41.             targetAngle -= rotationAmount;
    42.         }
    43.         else if(targetAngle <0)
    44.         {
    45.             transform.RotateAround(playerPos, Vector3.up, rotationAmount);
    46.             targetAngle += rotationAmount;
    47.         }
    48.    
    49.     }
    50. }
    I should've said "OR" use this method when speaking before.

    Above is a more complex way of doing it if you don't want to child your camera (usually for RTS this is the case).

    If you don't mind doing that the other way to do it is to child the camera to the object that is the focal point (player character) and use this code:

    Code (CSharp):
    1. private float targetAngle = 0;
    2. const float rotationAmount = 1.5f;
    3. public float rSpeed = 1.0f;
    4.  
    5. void Update () {
    6.         // Rotation method
    7.         if (Input.GetKeyDown(KeyCode.Q)) {
    8.             targetAngle -= 90.0f;
    9.         }
    10.         if (Input.GetKeyDown(KeyCode.E)) {
    11.             targetAngle += 90.0f;
    12.         }
    13.  
    14.         if(targetAngle !=0)
    15.         {
    16.             Rotate();
    17.         }
    18.     }
    19.  
    20. protected void Rotate()
    21.     {
    22.      
    23.  
    24.      
    25.         if (targetAngle>0)
    26.         {
    27.             transform.RotateAround(transform.position, Vector3.up, -rotationAmount);
    28.             targetAngle -= rotationAmount;
    29.         }
    30.         if(targetAngle <0)
    31.         {
    32.             transform.RotateAround(transform.position, Vector3.up, rotationAmount);
    33.             targetAngle += rotationAmount;
    34.         }
    35.      
    36.     }
    You don't "have" to use a protected void btw, just kept it for continuity sakes.
     
    Last edited: Aug 19, 2014