Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Magic-4e · Aug 28, 2018 at 10:23 AM · cameraclippingspherecast

How to zoom the camera when obstructed?

Hi I have made a working script to avoid camera clipping with the help of a sphere cast.

The problem is that it sometimes picks the wrong wall to lean against at certain angels.

Can anyone help me out?


Here is my code:

 using UnityEngine;
 using System.Collections;
 using System;
 
 public class Thir_Pers_CameraController : MonoBehaviour
 {
     // *Public*
     //Object to follow
     public Transform target;
     //Clamp angles (Keep it betwean 0 and 180)
     public float maxUp_dontPass180 = 0f;
     public float maxDown_dontPassZero = 0f;
     //Camera movement speed
     public float cameraSpeed = 1f;
     //Invertable axis
     public bool invertX = false;
     public bool invertY = false;
 
     // *Private*
     //Player input value variables
     private float camXinput = 0.0f;
     private float camYinput = 0.0f;
     //My up/down angle translation variable
     private float currentYaxis = 0;
     //Short version of clamp angles
     private float maxUp = 0;
     private float maxDown = 0;
     //Number versions of invertor axis
     private int invertorX = 1;
     private int invertorY = 1;
     //Camera
     //public Camera playerCamera;
     //Camera pivot points
     GameObject CameraPivotY;
     GameObject CameraPivotX;
     GameObject PlayerGravityPivot;
     //Objects with Ground tag
     //GameObject[] groundObjects;
     //Colliders of Ground
     //Collider[] groundColliders;
     //Camera frustum
     //Plane[] cameraFrustum;
     //Test frustum
     //public float frustumCollide;
     //Test far clip plane
     //public float farClipPlane;
     //Object that sphere cast hit's
     public GameObject sphereHitObject;
     //SphereCast radius
     public float sphereRadius;
     //SphereCast Max Distance
     public float sphereMaxDistance;
     //What has effect on sphereCast
     public LayerMask sphereLayerMask;
     //SphereCast origin
     private Vector3 sphereOrigin;
     //SphereCast direction
     private Vector3 sphereDirection;
     //Distance to object that has bean hit
     public float sphereHitDistance;
 
     void Start()
     {
         //Find cam X & Y pivots
         CameraPivotY = GameObject.FindGameObjectWithTag("CameraPivotY");
         CameraPivotX = GameObject.FindGameObjectWithTag("CameraPivotX");
         //Find graf pivot
         PlayerGravityPivot = GameObject.FindGameObjectWithTag("CameraGravityPivot");
         //Turn long into short clamp varibles
         maxUp = maxUp_dontPass180;
         maxDown = maxDown_dontPassZero;
         //Find camera Max distance
         sphereMaxDistance = transform.position.z;
     }
 
     void FixedUpdate()
     {
         sphereOrigin = CameraPivotY.transform.position;
         sphereDirection = -CameraPivotY.transform.forward;
         RaycastHit[] hit;
         hit = Physics.SphereCastAll(sphereOrigin, sphereRadius, sphereDirection, sphereMaxDistance, sphereLayerMask, QueryTriggerInteraction.UseGlobal);
         if (hit.Length != 0)
         {
             sphereHitObject = hit[0].transform.gameObject;
             sphereHitDistance = -hit[0].distance;
             transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, sphereHitDistance);
         }
         else
         {
             sphereHitObject = null;
             sphereHitDistance = -sphereMaxDistance;
             transform.localPosition = new Vector3(transform.localPosition.x, transform.localPosition.y, sphereHitDistance);
         }
         /*
         //Find all Ground objects
         groundObjects = GameObject.FindGameObjectsWithTag("Ground");
         //Get colliders of Ground objects
         groundColliders = new Collider[groundObjects.Length];
         for(int i = 0; i < groundObjects.Length; i++)
         {
             groundColliders[i] = groundObjects[i].GetComponent<Collider>();
         }
         //Get playerCamera frustum
         cameraFrustum = GeometryUtility.CalculateFrustumPlanes(playerCamera);
         //Change far clip plane into camera distance to player object
         cameraFrustum[5].distance = -Vector3.Dot(cameraFrustum[5].normal, transform.position);
         //Read far clip plane
         farClipPlane = cameraFrustum[5].distance;
         //Check if there is Ground in frustum
         for (int i = 0; i < groundColliders.Length; i++)
         {
             if (!GeometryUtility.TestPlanesAABB(cameraFrustum, groundColliders[i].bounds))
             {
                 if(i == groundColliders.Length - 1)
                 {
                     frustumCollide = 0;
                 }
             }
             else
             {
                 i = groundColliders.Length;
                 frustumCollide = 1;
             }
         }
         */
         //My user preference camera invertor script
         invertorX = (invertX) ? -1 : 1;
         invertorY = (invertY) ? -1 : 1;
 
 
         //Get cam control inputs
         camXinput = Input.GetAxis("Mouse X") * invertorX * cameraSpeed;
         camYinput = Input.GetAxis("Mouse Y") * invertorY * cameraSpeed;
         
 
         //*Camera move left and right (for some reson up and right are inverted)
         CameraPivotX.transform.Rotate(Vector3.up * camXinput, Space.Self);
         
         //My currentYaxis varible wich reads the left/right angle of my camera and translate's it to a straight 0 to 180 (it doosn't have a full 360 degrees but 2 halfs of the same value)
         if(CameraPivotY.transform.localEulerAngles.x >= 0 && CameraPivotY.transform.localEulerAngles.x <= 90) //Test if camera angle is in the second quarter (wich reads too low)
         {
             currentYaxis = CameraPivotY.transform.localEulerAngles.x + 90; //Set the angle higher
         }
         if(CameraPivotY.transform.localEulerAngles.x >= 270 && CameraPivotY.transform.localEulerAngles.x <= 360) //Test if camera angle is in the first quarter (wich reads too high)
         {
             currentYaxis = CameraPivotY.transform.localEulerAngles.x - 270; //Set the angle lower
         }
 
         //*Camera move up and down combined with a clamping script wich uses my currentYaxis varible to determine where to clamp (it can't use a clamp value higher than 180 or below 0 becouse of my variable issues mentioned earlier)
         if (camYinput < 0) //Check if player moves camera down
         {
             if (maxDown > (currentYaxis + camYinput)) //Test if player input causes camera angle to move past MaxDown
             {
                 CameraPivotY.transform.Rotate(Vector3.right * (currentYaxis - maxDown) * -1, Space.Self); //Set camera angle to Max
             }
             else
             {
                 CameraPivotY.transform.Rotate(Vector3.right * camYinput, Space.Self); //Move the camera
             }
         }
         else if (camYinput > 0) //Check if player moves camera up
         {
             if (maxUp < (currentYaxis + camYinput)) //Test if player input causes camera angle to move past MaxUp
             {
                 CameraPivotY.transform.Rotate(Vector3.right * (maxUp - currentYaxis), Space.Self); //Set camera angle to Max
             }
             else
             {
                 CameraPivotY.transform.Rotate(Vector3.right * camYinput, Space.Self); //Move the camera
             }
         }
     }
 }
 

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Magic-4e · Sep 03, 2018 at 10:44 AM 0
Share

Please someone?

I really can't figure out this bug.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

145 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Oblique Frustum Clipping 0 Answers

How to cut off these clouds on my camera? 1 Answer

Change the layer of trees on terrain? 0 Answers

Near Clipping Plane Affecting Rendering Massive Objects 1 Answer

Is it possible to render a solid color where the Camera clips to the Near Clipping Plane? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges