阻止相机移动通过物体 [英] Stop camera from moving through objects
问题描述
关于这个主题有很多问题,但是我找不到适合我的问题.
There is a lot of questions on this theme but i couldn't find one that is fitting me.
我有一个如下所示的摄像机移动脚本:
I have camera movement script which looks like this:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine.Networking;
[AddComponentMenu("Camera-Control/Smooth Mouse Look")]
public class SmoothMouseLook : NetworkBehaviour
{
[Header("Character camera.")]
private Camera myCamera;
[Header("Location where camera will always be looking at.")]
public Transform characterHead;
[Header("How fast will camera zoom work")]
public float cameraScrollSensitivity = 2f;
[Header("Default position of camera")]
public Vector3 cameraPos = new Vector3(0, 2, 0);
public Vector3 minPos = new Vector3(-4, -4, -5);
public Vector3 maxPos = new Vector3(4, 4, -1);
[Header("How fast will camera move on Y axis")]
public float cameraSensitivityY = 0.8f;
[Header("How fast will camera move on X axis")]
public float cameraSensitivityX = 0.8f;
public enum InvertX { False = 0, True = 1 }
public enum InvertY { False = 0, True = 1 }
public InvertX invertX = InvertX.False;
public InvertY invertY = InvertY.False;
void Start()
{
if (!isLocalPlayer)
return;
Camera.main.gameObject.transform.parent = this.transform;
Camera.main.gameObject.transform.localPosition = cameraPos;
myCamera = Camera.main;
myCamera.transform.LookAt(characterHead);
}
void Update()
{
if (!isLocalPlayer)
return;
Vector3 currPos = myCamera.transform.localPosition;
currPos.z += Input.GetAxis("Mouse ScrollWheel") * cameraScrollSensitivity;
if (currPos.z > maxPos.z)
currPos.z = maxPos.z;
if (currPos.z < minPos.z)
currPos.z = minPos.z;
if (Input.GetMouseButton(2))
{
if(invertY == 0)
currPos.y -= Input.GetAxis("Mouse Y") * cameraSensitivityY / 10;
else
currPos.y += Input.GetAxis("Mouse Y") * cameraSensitivityY / 10;
if (invertX == 0)
currPos.x += Input.GetAxis("Mouse X") * cameraSensitivityX / 10;
else
currPos.x -= Input.GetAxis("Mouse X") * cameraSensitivityX / 10;
if (currPos.y > maxPos.y)
currPos.y = maxPos.y;
if (currPos.y < minPos.y)
currPos.y = minPos.y;
if (currPos.x > maxPos.x)
currPos.x = maxPos.x;
if (currPos.x < minPos.x)
currPos.x = minPos.x;
}
myCamera.transform.localPosition = currPos;
myCamera.transform.LookAt(characterHead);
}
}
此脚本附加在角色上,相机是孩子.
This script is attached to character and camera is it's child.
正如标题所述,问题在于相机可以穿过网格(物体和地形).
Just as title says, problem is that camera can go through mesh (objects and terrain).
我尝试将剔除蒙版"设置为0.01和1(默认值为0.3),但没有任何反应.
I tried setting Culling mask to 0.01 and 1 (default is 0.3) but nothing happens.
我尝试添加对撞机,当我将摄像机推到它撞到的地面时,它可以工作,但不会让摄像机掉下来,但是会抬起角色:/
I tried adding collider which works BUT when i push camera to the ground it collides, doesn't let camera down but it lift character up :/
我唯一的想法是使用raycast对其进行编码,但是由于我不熟悉这一切,因此我想检查是否还有其他方法(例如使用对撞机但不抬起字符),因为这会花费很多时间我用raycast弄清楚了.
Only idea i have is coding it with raycast but since i am new to all this i wanted to check if there is any other way of doing it (like with collider but do not lift character) because it will take a lot of my time figuring it out with raycast.
推荐答案
您可以尝试执行此操作吗?如果您不希望像虚幻引擎中的相机那样穿过网格,则可以这样做
Could you try doing this things . If you mean not pass through meshes like the camera in Unreal Engine you can do it something like this
- 选择相机,进入检查器并将近裁剪平面设置为0.1
- 创建一个空的游戏对象.给一个空的盒子对撞机,并将其设置为相机的确切位置.最后,将空对象设置为Camera的父对象.现在,当您将其移为空时,它将移动相机,并且不会穿过墙壁.
希望这会有所帮助
这篇关于阻止相机移动通过物体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!