阻止相机移动物体

时间:2018-05-07 02:32:59

标签: c# unity3d camera

关于这个主题有很多问题,但我找不到适合我的问题。

我有相机移动脚本,如下所示:

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);
    }
}

此脚本附加到角色,相机是它的孩子。

正如标题所说,问题是相机可以穿过网格(物体和地形)。

我尝试将Culling mask设置为0.01和1(默认值为0.3),但没有任何反应。

我尝试添加碰撞器,但是当我将相机推到碰撞的地面时,它会起作用,不会让相机瘫痪但是它会提升角色:/

我只有想法是用raycast编码它,但因为我是所有这些的新手我想检查是否有任何其他方式这样做(比如使用对撞机但不提升角色)因为它需要很多我的时间用raycast来解决它。

1 个答案:

答案 0 :(得分:0)

你能尝试做这件事。如果你的意思是不像虚幻引擎中的相机一样通过网格你可以这样做

  1. 选择相机,转到检查器并将近剪裁平面设置为0.1
  2. 创建一个空的游戏对象。给空箱子对撞机,并将其设置到摄像机的确切位置。最后将空对象设置为Camera的父对象。现在当你移动空时,它移动相机,它不会穿过墙壁。
  3. 希望这有帮助