Search Unity

Stop rotating when the camera hits the floor

Discussion in 'Scripting' started by markpollard1, May 18, 2017.

  1. markpollard1

    markpollard1

    Joined:
    Apr 1, 2010
    Posts:
    70
    Trying to limit the Miny when i rotate the camera on the y when it hits the floor.

    Currently the camera does stop rotating when he hits the floor but the camera shakes like its fighting with some vector. If anybody can help? or need more info.

    Basically this is for a third person camera script which follows the player and it will rotate on x and y around the player, but it goes though the floor.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Third_Camera : MonoBehaviour {
    6.  
    7.   public static Third_Camera Instance;
    8.  
    9.   public Transform Target;
    10.  
    11.  
    12.   public float DistanceFromTarget = 20f;
    13.   public float tempDistanceFromTarget = 20f;
    14.   public float minY = 10;
    15.   public float StartY = 0;
    16.   public float maxY = 80;
    17.   public float Yoffset = 2.0f;
    18.   public float rotateSpeedLR = 0.5f;
    19.   public float rotateSpeedUPDOWN = 0.5f;
    20.  
    21.   public float MinHeightAboveFloor = 1;
    22.  
    23.   public bool hittingTerrian = false;
    24.  
    25.   public float TerrianDistance = 1;
    26.  
    27.   public GameObject TestSpawn;
    28.  
    29.   public LayerMask layerMask;
    30.  
    31.   public LayerMask TerrianLayerMask;
    32.   public float radius = 20;
    33.  
    34.   public float doubleClickTime = 0.2f;
    35.   public float clickTime = 0;
    36.   public float ScrollSpeed = 2f;
    37.  
    38.   private float angleY = 0;
    39.   private float angleX = 0;
    40.  
    41.   private bool firstClick = false;
    42.   private bool doubleClick = false;
    43.  
    44.   private Vector3 tempCameraPosition = Vector3.zero;
    45.  
    46.   public bool autoPan = false;
    47.   public bool canSeePlayer = true;
    48.  
    49.   private BoxCollider terrianCollider;
    50.  
    51.   float rotation;
    52.  
    53.   private Quaternion targetRotation;
    54.  
    55.  
    56.  
    57.  
    58.  
    59.  
    60.   void Awake()
    61.   {
    62.     Instance = this;
    63.     Target =Transform.FindObjectOfType<Third_Motor>().transform;
    64.     terrianCollider = GetComponent<BoxCollider>();
    65.   }
    66.  
    67.   // Use this for initialization
    68.   void Start () {
    69.     transform.localEulerAngles = new Vector3(StartY * 2,0,0);
    70.   }
    71.   void Update()
    72.   {
    73.     if(Input.GetKeyDown(KeyCode.LeftAlt))
    74.     {
    75.       autoPan =! autoPan;
    76.     }
    77.  
    78.     if(Input.GetMouseButton(1) || autoPan || Input.GetMouseButton(2))
    79.     {
    80.       RotateCamera();
    81.     }
    82.  
    83.  
    84.  
    85.   }
    86.  
    87.   // Update is called once per frame
    88.   void LateUpdate(){
    89.  
    90.     ///this is the scroll wheel adjust amount
    91.     float sw = Input.GetAxis("Mouse ScrollWheel");
    92.  
    93.     ///setting the distance from the target using the scroll wheel adjustment amount
    94.     DistanceFromTarget -= sw * Time.deltaTime *ScrollSpeed;
    95.     if(Input.GetKeyDown(KeyCode.B))
    96.     {
    97.       ResetCameraBehindPlayer();
    98.     }
    99.  
    100.     transform.position = GetTargetWthOffest(Yoffset) - transform.forward * DistanceFromTarget;
    101.  
    102.  
    103.     //CheckForCollision();
    104.   }
    105.  
    106.   float GetPlayerDistanceFromCamera()
    107.   {
    108.     return (Vector3.Distance(transform.position,Target.position));
    109.   }
    110.  
    111.  
    112.   float GetFloorDistance()
    113.   {
    114.     RaycastHit hit;
    115.  
    116.     if(Physics.Raycast( transform.position, Vector3.down, out hit,TerrianLayerMask))
    117.     {
    118.      // print(hit.distance);
    119.       Debug.DrawLine(transform.position, hit.point,Color.red);
    120.       return (hit.distance);
    121.     }
    122.  
    123.     print("return -1");
    124.     return -1;
    125.   }
    126.  
    127.   Vector3 GetTargetWthOffest(float YOffset)
    128.   {
    129.     Vector3 tempPosition = new Vector3(Target.position.x,Target.position.y + YOffset, Target.position.z);
    130.       return tempPosition;
    131.   }
    132.  
    133.   void RotateCamera()
    134.   {
    135.     float y;
    136.  
    137.     if(GetFloorDistance() < MinHeightAboveFloor)
    138.     {
    139.       y = transform.rotation.x;
    140.     }
    141.     else
    142.     {
    143.       y = minY;
    144.     }
    145.  
    146.     angleX += Input.GetAxis("Mouse X") * rotateSpeedLR;
    147.     angleY = Mathf.Clamp(angleY - Input.GetAxis("Mouse Y") * rotateSpeedUPDOWN, y,maxY);
    148.  
    149.     transform.rotation = Quaternion.Euler(angleY, angleX, 0f);
    150.  
    151.  
    152. }
    153.  
    154.   void CheckForCollision()
    155.   {
    156.  
    157.     float distance = GetPlayerDistanceFromCamera();
    158.  
    159.     RaycastHit hit;
    160.     Ray ray = new Ray(transform.position, transform.forward);
    161.  
    162.  
    163.     if(Physics.Raycast(ray,out hit, distance, layerMask))
    164.     {
    165.  
    166.       if(hit.transform.name != "Hero")
    167.       {
    168.        //print("something in the way move closer");
    169.  
    170.        RaycastHit hit2;
    171.  
    172.         Ray Secondray = new Ray(ray.GetPoint(100000), -ray.direction);
    173.         if(hit.collider.Raycast(Secondray, out hit2, 10000000))
    174.         {
    175.           transform.position = hit2.point;
    176.         }
    177.      
    178.       }
    179.  
    180.     }
    181.  
    182.   }
    183.  
    184.  
    185.   void ResetCameraBehindPlayer()
    186.   {
    187.     ////reset camera
    188.     Vector3 findPlayersDirection = Target.eulerAngles;
    189.  
    190.     transform.localEulerAngles = new Vector3(findPlayersDirection.x, findPlayersDirection.y, 0);
    191.     //transform.localEulerAngles = new Vector3(minY,0,0);
    192.     doubleClick = false;
    193.  
    194.   }
    195.  
    196.  
    197. }
     
    Last edited: May 18, 2017
  2. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
  3. markpollard1

    markpollard1

    Joined:
    Apr 1, 2010
    Posts:
    70
    OK i have managed to sort this out.