C# 摄像机碰撞检测
我的相机脚本有问题。摄影机使用指定为摄影机子对象的轴围绕播放器旋转。我正在为手机开发这个游戏,这是两个箭头,它们是触摸箭头,可以让相机左右旋转 问题是,当摄像机在墙或巨大物体后面时,什么也看不见。我寻找了一个解决方案,我发现许多开发人员使用了RaycastHit或类似的工具 这是我的代码,目标是在靠近墙时使摄影机靠近轴,以避免遮挡摄影机视图。有人能帮我吗C# 摄像机碰撞检测,c#,unity3d,camera,collision,raycasting,C#,Unity3d,Camera,Collision,Raycasting,我的相机脚本有问题。摄影机使用指定为摄影机子对象的轴围绕播放器旋转。我正在为手机开发这个游戏,这是两个箭头,它们是触摸箭头,可以让相机左右旋转 问题是,当摄像机在墙或巨大物体后面时,什么也看不见。我寻找了一个解决方案,我发现许多开发人员使用了RaycastHit或类似的工具 这是我的代码,目标是在靠近墙时使摄影机靠近轴,以避免遮挡摄影机视图。有人能帮我吗 public Transform target1; public Transform pivot; protected ButtonLeft
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);
}
好的方法是从轴到摄影机进行光线投射,若发现障碍物,将摄影机放在光线上,离轴稍微近一点,而不是击中点。如下所示:伪代码,未测试:
Vector3 origin = pivot.transform.position;
Vector3 direction = transform.position - pivot.transform.position;
float maxCameraDistance = 10;
Ray ray = new Ray(origin, direction);
RaycastHit hit;
if (Physics.Raycast(ray, maxCameraDistance))
{
Vector3 offsetFromObstacle = -direction.normalized * 0.1f;
transform.position = hit.point + offsetFromObstacle;
}
您可以在那里添加一个lerp,或者不直接将摄影机移动到命中点,而是移动到其正上方的点,在摄影机和轴之间保持一定距离。例如,在我的代码中设置位置后添加此行:transform.position=newvector3transform.position.x,5,transform.position.z;假设相机绕Y轴旋转,5是相机高度。