Search Unity

Somethings not right with this 3d camera script

Discussion in 'Scripting' started by Jocky4s, Sep 30, 2015.

  1. Jocky4s

    Jocky4s

    Joined:
    Apr 16, 2015
    Posts:
    11
    I'm pretty new to unity but iv done some coding in the past. I have got some code for an orbital camera that sets to my target. The camera works...but... Only if I have the right mouse button down. For some reason its not updating and staying following the target after I let go. I'm probably missing something extremely basic here but if someone could give me a point in the right direction that would be awesome :) I just doesnt do anything unless I have the right mouse button press down: it doesnt even scroll unless I have the mouse button pressed.
    Code (CSharp):
    1. public Transform playertarget;
    2.  
    3.     private float x = 0.0f;
    4.     private float y = 0.0f;
    5.  
    6.     private int mouseXSpeedMod = 5;
    7.     private int mouseYSpeedMod = 3;
    8.  
    9.     public float maxVeiwDistance = 25; // how far the camera zooms out
    10.     public float minVeiwDistance = 3; // How close the camera zooms in
    11.     private float distance = 3; // Starting distance from player
    12.     private float desiredDistance; // used for calculations
    13.     private float correctDistance; // used for calculations
    14.     public int zoomRate = 30; // How fast the camera will zoom
    15.  
    16.     public int lerpRate = 5;
    17.     public float playerTargetHight = 1.0f;
    18.     private float currentDistance;
    19.  
    20.  
    21.  
    22.  
    23.  
    24.  
    25.     void Start()
    26.     {
    27.         Vector3 angles = transform.eulerAngles;
    28.         x = angles.x;
    29.         y = angles.y;
    30.  
    31.         currentDistance = distance;
    32.         desiredDistance = distance;
    33.         correctDistance = distance;
    34.  
    35.  
    36.  
    37.  
    38.     }
    39.  
    40.     // Update is called once per frame
    41.     void Update()
    42.     {
    43.  
    44.     }
    45.  
    46.     void LateUpdate() //Update camera every frame
    47.     {
    48.  
    49.  
    50.  
    51.  
    52.  
    53.         if (Input.GetMouseButton(1))
    54.         {
    55.             x += Input.GetAxis("Mouse X") * mouseXSpeedMod;
    56.             y += Input.GetAxis("Mouse Y") * mouseYSpeedMod;
    57.         }
    58.  
    59.         else if (Input.GetAxis("vertical") != 0 || Input.GetAxis("Horizontal") != 0)
    60.         {
    61.             float targetRotationAngle = playertarget.eulerAngles.y;
    62.             float cameraRotationAngle = transform.eulerAngles.y;
    63.  
    64.             x = Mathf.LerpAngle(cameraRotationAngle, targetRotationAngle, lerpRate * Time.deltaTime);
    65.         }
    66.  
    67.  
    68.         y = ClampAngle(y, -50, 80);
    69.  
    70.         Quaternion rotation = Quaternion.Euler(y, x, 0);
    71.  
    72.         desiredDistance -= Input.GetAxis("Mouse ScrollWheel") * Time.deltaTime * zoomRate * Mathf.Abs(desiredDistance); // calcualtes the distance between teh camera and the player
    73.         desiredDistance = Mathf.Clamp(desiredDistance, minVeiwDistance, maxVeiwDistance);
    74.         correctDistance = desiredDistance;
    75.  
    76.         Vector3 position = playertarget.position - (rotation * Vector3.forward * desiredDistance);
    77.  
    78.         RaycastHit collisionHit;
    79.  
    80.         Vector3 playerTargetPosition = new Vector3(playertarget.position.x, playertarget.position.y + playerTargetHight, playertarget.position.z);
    81.  
    82.         bool isCorrected = false;
    83.         if (Physics.Linecast(playerTargetPosition, position, out collisionHit))
    84.         {
    85.             position = collisionHit.point;
    86.             correctDistance = Vector3.Distance(playerTargetPosition, position);
    87.             isCorrected = true;
    88.         }
    89.  
    90.         currentDistance = !isCorrected || correctDistance > currentDistance ? Mathf.Lerp(currentDistance, correctDistance, Time.deltaTime * zoomRate) : correctDistance;
    91.  
    92.         position = playertarget.position - (rotation * Vector3.forward * currentDistance + new Vector3(0, -playerTargetHight, 0));
    93.  
    94.  
    95.  
    96.  
    97.         transform.rotation = rotation; // when you call transform within the script, it looks for the transform the script is atacthed too.
    98.         transform.position = position;
    99.  
    100.     }
    101.     private static float ClampAngle(float angle, float min, float max)
    102.     {
    103.         if (angle < -360)
    104.         {
    105.             angle -= 360;
    106.         }
    107.         if (angle > 360)
    108.         {
    109.             angle += 360;
    110.         }
    111.         return Mathf.Clamp(angle, min, max);
    112.     }
     
    Last edited: Oct 1, 2015
  2. Polymorphik

    Polymorphik

    Joined:
    Jul 25, 2014
    Posts:
    599
    Look into Execute Order, FixedUpdate should be where all your Physics logic should be as it gets called a fixed number of times vs Update where it is variable. Knowing this, if you want to follow a target you need to update the following object every frame since you don't know where the target will be. Any changes will be updated since you are checking every frame. From what I can see, you are not doing that. Then again its hard to read your code since you are not using code tags. Please update your post so your code can be legible.
     
  3. Jocky4s

    Jocky4s

    Joined:
    Apr 16, 2015
    Posts:
    11
    Code tags have been put on sorry it was late :p Would I have to have bool values in fixed update?
     
    Last edited: Oct 1, 2015
  4. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    so only when you right clicking your x und y are changing.

    your elsif,
    just check with a Debug.Log if he is even going in here and what resuklts you geht.

    use some debug logs at all to check which values your variables have. or breakpoints if you can.
     
  5. Jocky4s

    Jocky4s

    Joined:
    Apr 16, 2015
    Posts:
    11
    Got it working :)
     
    martinmr likes this.
  6. martinmr

    martinmr

    Joined:
    Mar 25, 2015
    Posts:
    325
    can you tell us how you fixed it?
     
  7. Jocky4s

    Jocky4s

    Joined:
    Apr 16, 2015
    Posts:
    11
    It was actually just a simple typing error on my part.
    Code (CSharp):
    1. else if (Input.GetAxis("vertical") != 0 || Input.GetAxis("Horizontal") != 0)
    I have to change the lower case "v" in vertical to upper case :)