Unity渲染

时间:2018-01-03 20:55:28

标签: c# unity3d camera rendering

你好,我想问一个问题,我有一个狙击手,在大多数游戏中你用狙击手放大枪已经不见了但是我和大多数人使用2个摄像头1是为了世界而另一个是仅用于武器所以我们有一个层武器,我可以放大,让我说我禁用gunCamera(只提供武器的相机)所以我们有一个玩家面前我的枪用层武器我不会看到他的武器我怎么能修复它。这是脚本(我试图减少那些不会起作用的视图):

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Sniper : MonoBehaviour {

    private float range;
    private float damage;

    private GameObject scope;
    private GameObject crossHair;
    private bool isScoped;
    private bool canFire;

    private GameObject weaponCamera;
    private Camera gunCamera;
    private Camera fpsCamera;
    private float normalFOV;
    private float zoomFOV;
    private float zoomGunFov;
    private float normalGunFov;

    // Use this for initialization
    private void Awake()
    {
        fpsCamera = Camera.main;
        canFire = true;
        isScoped = false;
        weaponCamera = transform.parent.parent.gameObject.transform.Find("GunCamera").gameObject;
        crossHair = transform.parent.parent.parent.parent.gameObject.transform.Find("CrossHairs").gameObject.transform.Find("cursor").gameObject;
        scope = transform.parent.parent.parent.parent.gameObject.transform.Find("CrossHairs").gameObject.transform.Find("scope").gameObject;
    }
    void Start() {
        gunCamera = weaponCamera.GetComponent<Camera>();
        crossHair.SetActive(false);
        scope.SetActive(false);
        damage = 100f;
        range = 10000f;
        zoomFOV = 15f;
        zoomGunFov = 1f;
        normalFOV = fpsCamera.fieldOfView;
        normalGunFov = gunCamera.fieldOfView;
    }

    // Update is called once per frame
    void Update() {
        Shoot();
        Zoom();
    }

    private void Shoot()
    {
        if (Input.GetButtonDown("Fire1") && canFire)
        {
            ShootRay();
            StartCoroutine(ReloadShot());
        }
    }

    private void Zoom()
    {
        if (Input.GetButtonDown("Fire2"))
        {
            isScoped = !isScoped;
            scope.SetActive(isScoped);

        }

        if (isScoped)
        {
            OnScoped();
        }
        else
        {
            NotScoped();
        }
    }

    private void OnScoped()
    { 
        fpsCamera.fieldOfView = zoomFOV;
        gunCamera.fieldOfView = zoomGunFov;
    }

    private void NotScoped()
    {
        fpsCamera.fieldOfView = normalFOV;
        gunCamera.fieldOfView = normalGunFov;
    }

    private void ShootRay()
    {
        RaycastHit hit;
        if(Physics.Raycast(fpsCamera.transform.position,fpsCamera.transform.forward,out hit, range))
        {
            Health_Armor target = hit.transform.GetComponent<Health_Armor>();

            if (target != null)
            {
                target.TakeDamage(damage);
            }
        }
    }

    private void OnDisable()
    {
        isScoped = false;
        scope.SetActive(false);
        crossHair.SetActive(true);
        fpsCamera.fieldOfView = normalFOV;
        gunCamera.fieldOfView = normalGunFov;
    }

    private void OnEnable()
    {      
        canFire = true;
        crossHair.SetActive(false);
    }

    IEnumerator ReloadShot()
    {
        canFire = false;
        yield return new WaitForSeconds(1f);
        canFire = true;

    }


}

enter image description here

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

使用2层,例如:

  • WeaponPlayer - 对于localPlayer,仅在本地播放器
  • 上按脚本设置此图层
  • WeaponOpponent - 对于其他玩家,将其设为默认

将你的Zoomed相机设置为不渲染WeaponPlayer