Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed Aug 28, 2018 at 09:12 AM by Magic-4e for the following reason:

I think I made a mess of my questions. 1 is stuck in moderation and this one wont really get awnserd.

avatar image
0
Question by Magic-4e · Jun 08, 2018 at 11:57 PM · cameraclippingfrustrumspherecastall

How do I zoom the camera when it is in danger of colliding or being obstructed

I wan't to know how to make SphereCastAll work.

At the moment I am getting an error: Cannot implicitly convert UnityEngine.RaycastHit to bool

Any ideas?

(Original question: Hi. I am trying to find a way to detect objects that are in between my camera and my character.

I thought I could use the frustum of my camera as a template for a collision detection box and now I am trying to figure out a way to resize it to only detect what's in between my camera and my character, but when I try to scale the far clipping plane back I still detect collision from all my objects except when I point the camera straight up.

Can somebody tell me what's going on?

And maybe how I should detect objects?)

(Edit: I updated my code with the last answers fix.)

(Edit: I don't want to use the frustum anymore because it becomes unreliable when resized so I wil use a SphereCast instead)

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;
         if(Physics.SphereCastAll(sphereOrigin, sphereRadius, sphereDirection, sphereMaxDistance, sphereLayerMask, QueryTriggerInteraction.UseGlobal))
         {
             sphereHitObject = hit.transform.gameObject;
             sphereHitDistance = -hit.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
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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Bunny83 · Jun 07, 2018 at 02:18 AM

This line doesn't seem to make much sense:

 cameraFrustum[5].distance = (100 / cameraFrustum[5].distance) * transform.position.z;

You know that the distance of a plane is the distance to the world origin, not the distance to the camera position. See this image:

FrustumPlanes

A mathematical plane is an infinite plane which is given in it's general form (normal and distance from origin). Also see the documentation of the Plane struct


From your question and that equation it's hard to tell what you want to achieve here. Do you want to ignore the far clipping plane? Do you want to move the far clipping plane to the position of your player?


If it's the last one you want to calculate the new distance like this:

 cameraFrustum[5].distance = -Vector3.Dot(cameraFrustum[5].normal, transform.position);



frustumplanes.png (6.3 kB)
Comment
Add comment · Show 8 · Share
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 Bunny83 · Jun 07, 2018 at 02:29 AM 0
Share

If you want to know what CalculateFrustumPlanes actually does, see my code example in the comment below my answer. This may even be the better way to calculate the planes as it's garbage free since you can reuse a single Plane array.

avatar image Bunny83 Bunny83 · Jun 07, 2018 at 02:30 AM 0
Share

ps: Unity Answers sucks at perma-linking comments ...

avatar image Magic-4e Bunny83 · Jun 07, 2018 at 06:43 AM 0
Share

Wouw thanks you made it much clearer how it works. (I did not really find a good explanation on the internet)

I already thought it was a value relative to the world axis, but was not sure. (I tryed: cameraFrustum[5].distance -= (cameraFrustum[5].distance - transform.position.z); but it did not work :( .....)

Anyway yes I do want to set the far plane to the player characters position. (I hope the sides will scale with it; the internet did not explain that either)

I am gonna use the code you suggested me.

Say what is the Dot variable? (Hope you can give a clear explanation on that too)

PS: I looked at the unity manual of Vector3.Dot for a second but its still a bit fuzzy to me.

Show more comments
avatar image Magic-4e · Jul 15, 2018 at 01:06 PM 0
Share

I accepted your answer because this is an old tread and I fear that unity doesn't like the fact that I ask questions but don't acept awnsers.

But I am not fully helpt yet. I continue on a different thread:

https://answers.unity.com/questions/1516850/how-to-zoom-the-camera-when-obstructed.html?childToView=1519297#comment-1519297

Follow this Question

Answers Answers and Comments

139 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

Related Questions

How can I know if a gameObject is 'seen' by a particular camera? 11 Answers

Planar reflection is getting cut off when using camera with an oblique viewing frustrum 0 Answers

Editor's Camera Clipping Plane 12 Answers

When do shaders compute? 1 Answer

Procedurally generated mesh disappears from certain camera angles 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