C# Unity如何检查主摄像机是否看到物体
我在做这样的事情,物体在平面上移动,相机在它的中心。我得到了它,所以相机随着鼠标旋转,当主相机看到游戏对象时,它停止。所以我使用了onbecamevisible()和onbecameinvisible()函数,但这适用于任何相机,包括场景视图。我如何使其在仅由主游戏摄像机看到时停止C# Unity如何检查主摄像机是否看到物体,c#,unity3d,camera,gameobject,C#,Unity3d,Camera,Gameobject,我在做这样的事情,物体在平面上移动,相机在它的中心。我得到了它,所以相机随着鼠标旋转,当主相机看到游戏对象时,它停止。所以我使用了onbecamevisible()和onbecameinvisible()函数,但这适用于任何相机,包括场景视图。我如何使其在仅由主游戏摄像机看到时停止 using System.Collections; using System.Collections.Generic; using UnityEngine; public class cubeMove : Mono
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class cubeMove : MonoBehaviour,moveObject
{
Camera cam;
public Transform checkedObject;
void Start()
{
cam = GetComponent<Camera>();
}
void Update()
{
Vector3 viewPos = cam.WorldToViewportPoint(checkedObject.position);
if ()
move();
}
public void move()
{
transform.Rotate(new Vector3(15, 30, 45) * Time.deltaTime);
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类立方体移动:单行为,移动对象
{
摄像机;
公共转换检查对象;
void Start()
{
cam=GetComponent();
}
无效更新()
{
Vector3 viewPos=cam.WorldToViewportPoint(检查对象位置);
如果()
move();
}
公开作废动议()
{
变换.旋转(新矢量3(15,30,45)*时间增量);
}
}
这是我的相机脚本
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Camera : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationX = 0F;
float rotationY = 0F;
private List<float> rotArrayX = new List<float>();
float rotAverageX = 0F;
private List<float> rotArrayY = new List<float>();
float rotAverageY = 0F;
public float frameCounter = 20;
Quaternion originalRotation;
void Update()
{
if (axes == RotationAxes.MouseXAndY)
{
rotAverageY = 0f;
rotAverageX = 0f;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotArrayY.Add(rotationY);
rotArrayX.Add(rotationX);
if (rotArrayY.Count >= frameCounter)
{
rotArrayY.RemoveAt(0);
}
if (rotArrayX.Count >= frameCounter)
{
rotArrayX.RemoveAt(0);
}
for (int j = 0; j < rotArrayY.Count; j++)
{
rotAverageY += rotArrayY[j];
}
for (int i = 0; i < rotArrayX.Count; i++)
{
rotAverageX += rotArrayX[i];
}
rotAverageY /= rotArrayY.Count;
rotAverageX /= rotArrayX.Count;
rotAverageY = ClampAngle(rotAverageY, minimumY, maximumY);
rotAverageX = ClampAngle(rotAverageX, minimumX, maximumX);
Quaternion yQuaternion = Quaternion.AngleAxis(rotAverageY, Vector3.left);
Quaternion xQuaternion = Quaternion.AngleAxis(rotAverageX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
rotAverageX = 0f;
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotArrayX.Add(rotationX);
if (rotArrayX.Count >= frameCounter)
{
rotArrayX.RemoveAt(0);
}
for (int i = 0; i < rotArrayX.Count; i++)
{
rotAverageX += rotArrayX[i];
}
rotAverageX /= rotArrayX.Count;
rotAverageX = ClampAngle(rotAverageX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis(rotAverageX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else
{
rotAverageY = 0f;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotArrayY.Add(rotationY);
if (rotArrayY.Count >= frameCounter)
{
rotArrayY.RemoveAt(0);
}
for (int j = 0; j < rotArrayY.Count; j++)
{
rotAverageY += rotArrayY[j];
}
rotAverageY /= rotArrayY.Count;
rotAverageY = ClampAngle(rotAverageY, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis(rotAverageY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}
}
void Start()
{
Rigidbody rb = GetComponent<Rigidbody>();
if (rb)
rb.freezeRotation = true;
originalRotation = transform.localRotation;
}
public static float ClampAngle(float angle, float min, float max)
{
angle = angle % 360;
if ((angle >= -360F) && (angle <= 360F))
{
if (angle < -360F)
{
angle += 360F;
}
if (angle > 360F)
{
angle -= 360F;
}
}
return Mathf.Clamp(angle, min, max);
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
公共类摄像机:单一行为{
公共枚举循环数{MouseXAndY=0,MouseX=1,MouseY=2}
公共旋转轴=旋转轴.MouseXAndY;
公共浮子灵敏度x=15F;
公众浮标灵敏度yy=15F;
公共浮动最小值x=-360F;
公共浮动最大值x=360F;
公共浮动最小值=-60F;
公共浮动最大值=60F;
浮动旋转x=0F;
浮动旋转Y=0F;
私有列表rotArrayX=新列表();
浮动平均值x=0F;
私有列表rotarayy=新列表();
浮动平均值Y=0F;
公共浮点帧计数器=20;
四元数原始旋转;
无效更新()
{
如果(轴==旋转轴.MouseXAndY)
{
旋转平均=0f;
rotAverageX=0f;
旋转Y+=输入.GetAxis(“鼠标Y”)*灵敏度yy;
rotationX+=Input.GetAxis(“鼠标X”)*灵敏度X;
旋转阵列。添加(旋转);
添加(旋转X);
如果(rotArrayY.Count>=帧计数器)
{
rotArrayY.RemoveAt(0);
}
if(rotArrayX.Count>=帧计数器)
{
rotArrayX.RemoveAt(0);
}
for(int j=0;j=帧计数器)
{
rotArrayX.RemoveAt(0);
}
for(int i=0;i=帧计数器)
{
rotArrayY.RemoveAt(0);
}
for(int j=0;j=-360F)和((角度360F)
{
角度-=360F;
}
}
返回数学夹具(角度、最小值、最大值);
}
}
球形移动
public class sphereMove : MonoBehaviour,moveObject {
public float delta = 1.5f;
public float speed = 2.0f;
private Vector3 startPos;
public UnityEngine.Camera cam;
public Transform checkedObject;
bool isMoving;
void Start()
{
isMoving = true;
cam = Camera.main;
}
void Update()
{
Vector3 viewPos = cam.WorldToViewportPoint(checkedObject.position);
if (viewPos.x > 0 && viewPos.x <= 1 && viewPos.y >= 0 && viewPos.y <= 1 && viewPos.z > 0)
isMoving = false;
else
isMoving = true;
if (isMoving)
move();
}
public void move()
{
Vector3 v = startPos;
v.x += delta * Mathf.Sin(Time.time * speed);
transform.position = v;
}
公共类sphereMove:MonoBehavior,moveObject{
公共浮动增量=1.5f;
公共浮子速度=2.0f;
私人Vector3 startPos;
公共单元发动机;摄像机;
公共转换检查对象;
布尔·伊斯莫温;
void Start()
{
isMoving=真;
cam=Camera.main;
}
无效更新()
{
Vector3 viewPos=cam.WorldToViewportPoint(检查对象位置);
如果(viewPos.x>0&&viewPos.x=0&&viewPos.y 0)
isMoving=假;
其他的
isMoving=真;
如果(正在移动)
move();
}
公开作废动议()
{
Vector3 v=起始点;
v、 x+=增量*数学正弦(时间*速度);
transform.position=v;
}
}您可以使用该函数,使用主摄像机调用该函数,并在参数中给出要检查的对象的位置。如果结果向量的x和y坐标值在0和1之间,且z值优于0,则表示摄像机可以看到对象的中心
UnityEngine.Camera cam;
bool isMoving;
void Start()
{
cam = UnityEngine.Camera.main;
isMoving = true;
}
void Update()
{
Vector3 viewPos = cam.WorldToViewportPoint(checkedObject.position);
if (viewPos.x >= 0 && viewPos.x <= 1 && viewPos.y >= 0 && viewPos.y <= 1 && viewPos.z > 0)
{
// Your object is in the range of the camera, you can apply your behaviour
isMoving = false;
}
else
isMoving = true;
if(isMoving)
Move();
}
UnityEngine.Camera-cam;
布尔·伊斯莫温;
void Start()
{
cam=UnityEngine.Camera.main;
isMoving=真;
}
无效更新()
{
Vector3 viewPos=cam.WorldToViewportPoint(检查对象位置);
如果(viewPos.x>=0&&viewPos.x=0&&viewPos.y 0)
{
//您的对象在摄影机的范围内,您可以应用您的行为
isMoving=假;
}
其他的
isMoving=真;
如果(正在移动)
Move();
}
建议的方法在您的案例中不起作用,因为您创建了一个名为Camera
w的类
public static class CameraEx
{
public static bool IsObjectVisible(this UnityEngine.Camera @this, Renderer renderer)
{
return GeometryUtility.TestPlanesAABB(GeometryUtility.CalculateFrustumPlanes(@this), renderer.bounds);
}
}
// UnityEngine.Camera cam1;
// UnityEngine.Camera cam2;
void Update()
{
bool isVisibleForCamera1 = cam1.IsObjectVisible(GetComponent<MeshRenderer>());
bool isVisibleForCamera2 = cam2.IsObjectVisible(GetCoponent<SpriteRenderer>());
}