你好,所以我想问一个问题,我有一个狙击手,在大多数游戏中,你用狙击手放大枪不见了,但我和大多数人使用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;
}
}



发布于 2018-01-04 16:58:41
使用2层,例如:
默认
并将您的变焦相机设置为不渲染WeaponPlayer
https://stackoverflow.com/questions/48084997
复制相似问题