- Home /
 
Camera collision detection.
Hi everybody, i'm in trouble with my Camera script. Camera rotates around player by using a pivot. assigned as a child of the camera. This works with 2 arrows (i'm developing this game for mobiles, so they're touch arrows) that allows camera to rotate left and right; the problem is when the camera goes behind a wall or a huge object and can't see nothing. I watched for some solution and I see that many developers used the RaycastHit or something similar. here's my code, the goal is that camera goes close to the pivot when hit a wall. Can anyone help me?
 public Transform target1;
     public Transform pivot;
 
 
     protected ButtonLeft buttonLeft;
     protected ButtonRight buttonRight;
     
 
     public Vector3 offset;
     public bool useOffsetValues;
 
     
     public float rotateSpeed;
 
     
 
     private void Start()
     {
 
         buttonLeft = FindObjectOfType<ButtonLeft>();
         buttonRight = FindObjectOfType<ButtonRight>();
         
         if (!useOffsetValues)
         {
             offset = target1.position - transform.position;
             
         }
 
         pivot.transform.position = target1.transform.position;
         
         
         //pivot.transform.parent = target.transform;
 
         //USE IF U WANT TO DISAPPEAR THE CURSOR
         //Cursor.lockState = CursorLockMode.Locked;
 
         //pivot.transform.parent = target.transform;
         pivot.transform.parent = null;
 
         // usa questa dopo la costruzione del livello1
         //pivot.transform.position = target.transform.position;
         
 
         
     }
 
     
 
 
     private void Update()
     {
         
 
         pivot.transform.position = target1.transform.position;
 
        
         
         
 
         if (buttonLeft.Pressed)
         {
             pivot.Rotate(0, -90 * Time.deltaTime, 0);
             Debug.Log("rotate left");
         }
 
         if (buttonRight.Pressed)
         {
             pivot.Rotate(0, 90 * Time.deltaTime, 0);
             Debug.Log("rotate left");
         }
 
         Ray ray = new Ray(pivot.transform.position, pivot.transform.position - transform.position);
         RaycastHit hit;
 
         
 
 
             /*float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
             float horizontal = Input.GetAxis("Mouse X") * rotateSpeed;
             pivot.Rotate(0, horizontal, 0);
             pivot.Rotate(0, horizontal, 0);
              Use this to make the camera rotate on Mouse Y axes*/
             /*float vertical = Input.GetAxis("Mouse Y") * rotateSpeed;
             target.Rotate(vertical, 0, 0); */
 
             //move camera based on the current rotation of the target and the original offset
 
 
             float desiredYAngle = pivot.eulerAngles.y;
         //Use this float to set the x angle of player
         float desiredXAngle = pivot.eulerAngles.x;
         //Use this rotation only if you want to rotate on Y
         //Quaternion rotation = Quaternion.Euler(0, desiredYAngle, 0);
         //Use this if u want to rotate up&down on x axes
         Quaternion rotation = Quaternion.Euler(desiredXAngle, desiredYAngle, 0);
         transform.position = target1.position - (rotation * offset);
         
 
         
         //transform.position = target.position - offset;
 
         transform.LookAt(target1);
         
         
 
 
     }
 
              Your answer
 
             Follow this Question
Related Questions
Can someone modify this for me? 0 Answers
Camera bounce back? 0 Answers
default cylinder to indicate hit.normal 1 Answer
Ray is not rotating with Object using Physics.Raycast 1 Answer
Raycasting to detect which side of collider is hit 2 Answers