- Home /
I think I made a mess of my questions. 1 is stuck in moderation and this one wont really get awnserd.
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
}
}
}
}
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:
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);
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.
ps: Unity Answers sucks at perma-linking comments ...
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.
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:
Follow this Question
Related Questions
Setting a cube to be exactly size of intersecting camera view plane 1 Answer
Is there any way to create a fade effect in order to hide what isn't rendered by the camera? 0 Answers
3rd Person Camera clips through walls 0 Answers
Reduce size of plane frustrum in code 1 Answer
Oblique Frustrum Camera (horizontal X, vertical Z) 0 Answers