- Home /
 
Third person Camera stop it from going lower than the terrian
 using System.Collections; using System.Collections.Generic; using UnityEngine;
 public class Third_Camera : MonoBehaviour {
 
   public static Third_Camera Instance;
 
   public Transform Target;
 
 
   public float DistanceFromTarget = 20f;
   public float tempDistanceFromTarget = 20f;
   public float minY = 10;
   public float StartY = 0;
   public float maxY = 80;
   public float Yoffset = 2.0f;
   public float rotateSpeedLR = 0.5f;
   public float rotateSpeedUPDOWN = 0.5f;
 
   public float MinHeightAboveFloor = 1;
 
   public bool hittingTerrian = false;
 
   public float TerrianDistance = 1;
 
   public GameObject TestSpawn;
 
   public LayerMask layerMask;
 
   public LayerMask TerrianLayerMask;
   public float radius = 20;
 
   public float doubleClickTime = 0.2f;
   public float clickTime = 0;
   public float ScrollSpeed = 2f;
 
   private float angleY = 0;
   private float angleX = 0;
 
   private bool firstClick = false;
   private bool doubleClick = false;
 
   private Vector3 tempCameraPosition = Vector3.zero;
 
   public bool autoPan = false;
   public bool canSeePlayer = true;
 
   private BoxCollider terrianCollider;
 
   float rotation;
 
 
 
 
   void Awake()
   {
     Instance = this;
     Target =Transform.FindObjectOfType<Third_Motor>().transform;
     terrianCollider = GetComponent<BoxCollider>();
   }
 
   // Use this for initialization
   void Start () {
     transform.localEulerAngles = new Vector3(StartY * 2,0,0);
   }
   
   void Update()
   {
     if(Input.GetKeyDown(KeyCode.LeftAlt))
     {
       autoPan =! autoPan;
     }
   }
 
   // Update is called once per frame
   void LateUpdate(){
 
     ///this is the scroll wheel adjust amount
     float sw = Input.GetAxis("Mouse ScrollWheel");
 
     ///setting the distance from the target using the scroll wheel adjustment amount
     DistanceFromTarget -= sw * Time.deltaTime *ScrollSpeed;
   
     if(Input.GetKeyDown(KeyCode.B))
     {
       ResetCameraBehindPlayer();
     }
       
     if(Input.GetMouseButton(1) || autoPan || Input.GetMouseButton(2))
     {
       RotateCamera();
     }
 
     float floorDistance = GetFloorDistance();
 
     if(floorDistance <= MinHeightAboveFloor)
     {
       print( "reached min floor height");
 
      
     }
    
     
       ///always makes sure the position is the distance away from the play so we get a circle effect
       transform.position = GetTargetWthOffest(Yoffset) - transform.forward * DistanceFromTarget;
     
   
     CheckForCollision();
   }
 
   float GetPlayerDistanceFromCamera()
   {
     return (Vector3.Distance(transform.position,Target.position));
   }
 
 
   float GetFloorDistance()
   {
     RaycastHit hit;
 
     if(Physics.Raycast( transform.position, Vector3.down, out hit,TerrianLayerMask))
     {
      // print(hit.distance);
       Debug.DrawLine(transform.position, hit.point,Color.red);
       return (hit.distance);
     }
 
     return -1;
   }
 
   Vector3 GetTargetWthOffest(float YOffset)
   {
     Vector3 tempPosition = new Vector3(Target.position.x,Target.position.y + YOffset, Target.position.z); 
       return tempPosition;
   } 
 
   void RotateCamera()
   {
     angleY = Mathf.Clamp(angleY - Input.GetAxis("Mouse Y") * rotateSpeedUPDOWN, minY,maxY);
     angleX += Input.GetAxis("Mouse X") * rotateSpeedLR;
 
     transform.localEulerAngles = new Vector3(angleY,angleX,0);
   }
     
   void CheckForCollision()
   {
 
     float distance = GetPlayerDistanceFromCamera();
 
     RaycastHit hit;
     Ray ray = new Ray(transform.position, transform.forward);
 
 
     if(Physics.Raycast(ray,out hit, distance, layerMask))
     {
       
       if(hit.transform.name != "Hero")
       {
        //print("something in the way move closer");
 
        RaycastHit hit2;
 
         Ray Secondray = new Ray(ray.GetPoint(100000), -ray.direction);
         if(hit.collider.Raycast(Secondray, out hit2, 10000000))
         {
           transform.position = hit2.point;
         }
           
       }
      
     }
      
   }
     
 
   void ResetCameraBehindPlayer()
   {
     ////reset camera
     Vector3 findPlayersDirection = Target.eulerAngles;
 
     transform.localEulerAngles = new Vector3(findPlayersDirection.x, findPlayersDirection.y, 0);
     //transform.localEulerAngles = new Vector3(minY,0,0);
     doubleClick = false;
 
   }
 
 
 }
 
               Hi I am using the above code to Make the number 1 in the picture work. The Camera rotates around the player using the x and y cords. I am trying to get the camera to rotate as in picture 2 where the camera will not go though the terrain. The camera will stay at a certain height when it gets to that height point away from the terrain. I have got all the ray casts in so i know how far the camera is away from the terrain but i cannot seam to limit the camera's y vector so that it does enter the floor.
Can anybody help?
Your answer
 
             Follow this Question
Related Questions
I want the camera, which is following my spaceship, to rotate to wherever the cursor is looking 0 Answers
Camera following player gameObject,Camera follows player, with script attached. 1 Answer
fixed rotation around object 1 Answer
Forward and back movements with a camera emulating an isometric view 1 Answer