Search Unity

Moba Styled Camera.

Discussion in 'Scripting' started by BubyMB, Jul 26, 2016.

  1. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    Hey Guys! I am making a script for a little moba camera. and I can't really visualize how to do a rotation lock. for example instead of always following the back of a player, it is always faces north(for example) but still follows the player. I have done this script so far..
    Code (csharp):
    1.  
    2.  
    3.  
    4. using UnityEngine;
    5. using System.Collections;
    6.  
    7. [AddComponentMenu("Camera-Control")]
    8.  
    9. public class Follow : MonoBehaviour
    10. {
    11.     public Transform target;
    12.     public float distance = 10.0f;
    13.     public float height = 5.0f;
    14.     public float heightDamping = 2.0f;
    15.     public float rotationDamping = 3.0f;
    16.  
    17.     public bool FreeCam = false;
    18.     void LateUpdate() {
    19.  
    20.         if(!FreeCam){
    21.  
    22.         if (!target)
    23.             return;
    24.  
    25.         float wantedRotationAngle = target.eulerAngles.y;
    26.         float wantedHeight = target.position.y + height;
    27.         float currentRotationAngle = transform.eulerAngles.y;
    28.         float currentHeight = transform.position.y;
    29.  
    30.         currentRotationAngle = Mathf.LerpAngle(currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
    31.  
    32.         currentHeight = Mathf.Lerp(currentHeight, wantedHeight, heightDamping * Time.deltaTime);
    33.  
    34.         Quaternion currentRotation = Quaternion.Euler(0, currentRotationAngle, 0);
    35.  
    36.         transform.position = target.position;
    37.         transform.position -= currentRotation * Vector3.forward * distance;
    38.  
    39.         transform.position = new Vector3(transform.position.x, currentHeight, transform.position.z);
    40.         transform.LookAt(target);
    41.         }
    42.         else
    43.         {
    44.             if (Input.GetKey(KeyCode.W))
    45.             {
    46.                 Vector3 posz = transform.position;
    47.                 posz.z = posz.z + 1;
    48.                 transform.position = posz;
    49.             }
    50.             if (Input.GetKey(KeyCode.S))
    51.             {
    52.                 Vector3 posz = transform.position;
    53.                 posz.z = posz.z - 1;
    54.                 transform.position = posz;
    55.             }
    56.             if (Input.GetKey(KeyCode.A))
    57.             {
    58.                 Vector3 posx = transform.position;
    59.                 posx.x = posx.x - 1;
    60.                 transform.position = posx;
    61.             }
    62.             if (Input.GetKey(KeyCode.D))
    63.             {
    64.                 Vector3 posx = transform.position;
    65.                 posx.x = posx.x + 1;
    66.                 transform.position = posx;
    67.             }
    68.         }
    69.     }
    70.  
    71.     void Update()
    72.     {
    73.         if(Input.GetKeyUp(KeyCode.F))
    74.         {
    75.             if (FreeCam)
    76.             {
    77.                 Debug.Log("FreeCam false");
    78.                 FreeCam = false;
    79.             }else
    80.             {
    81.                 Debug.Log("FreeCam true");
    82.                 FreeCam = true;
    83.             }
    84.         }
    85.     }
    86. }
    87.  
    but this code only follows the back of the player.
     
    Last edited: Jan 7, 2017
  2. Vedrit

    Vedrit

    Joined:
    Feb 8, 2013
    Posts:
    505
    If you switch from FreeCam true to false during play, and turn the character, the camera rotates with? Is the camera set as a child to the character?
     
  3. BubyMB

    BubyMB

    Joined:
    Jun 6, 2016
    Posts:
    140
    No, it is not a child of the player. It is an individual script that is following the player
     
unityunity