FPS中武器的位置调整

以下是对FPS插件中Ironsights脚本的简化版,此简化版更能够看清楚这个脚本的真正功能,其实这个脚本主要负责武器的位置调整的

//Ironsights.cs by Azuline Studios© All Rights Reserved
//Adjusts weapon position and bobbing speeds and magnitudes 
//for various player states like zooming, sprinting, and crouching.
using UnityEngine;
using System.Collections;

public class Ironsights : MonoBehaviour {
	public GameObject playerObj;
	public GameObject weaponObj;
	public GameObject CameraObj;
	public Camera WeapCameraObj;
	public GameObject gunObj;
	public Transform gun;
	private Transform mainCamTransform;
		
	private float nextPosX = 0.0f;
	private float nextPosY = 0.0f;
	private float newPosX = 0.0f;
	private float newPosY = 0.0f;
	private float dampVelocity = 0.0f;
	private float dampVelocity2 = 0.0f;
	private float dampVelocity3 = 0.0f;
	private Vector3 tempGunPos = Vector3.zero;
		
	public float defaultFov = 75.0f;
	public float sprintFov = 85.0f;
	private float nextFov = 75.0f;
	private float newFov = 75.0f;
	private float FovSmoothSpeed = 0.15f;
		
	
	public enum zoomType{
		hold,
		toggle,
		both
	}
	public zoomType zoomMode = zoomType.both;
	public AudioClip sightsUpSnd;
	public AudioClip sightsDownSnd;
	public bool zoomSfxState = true;
	public bool reloading = false;
	public float WalkBobSpeed = 12f;
	
	public float CrouchBobSpeed = 8f;
	public float ZoomBobSpeed = 8f;
	public float SprintBobSpeed = 19f;
		

	private float weaponUnzoomXPositionSprintAmt = 0.0f;

	public float gunAnimTime = 0.0f;
	
	void Update (){
		mainCamTransform = Camera.main.transform;
		SmoothMouseLook SmoothMouseLook = CameraObj.GetComponent<SmoothMouseLook>();
		PlayerWeapons PlayerWeaponsComponent = weaponObj.GetComponent<PlayerWeapons>();
		FPSRigidBodyWalker FPSWalker = playerObj.GetComponent<FPSRigidBodyWalker>();
		WeaponBehavior WeaponBehaviorComponent = gunObj.GetComponent<WeaponBehavior>();
		VerticalBob VerticalBob = playerObj.GetComponent<VerticalBob>();
		
		FPSPlayer FPSPlayerComponent = playerObj.GetComponent<FPSPlayer>();

        Camera.main.fieldOfView = Mathf.SmoothDamp(Camera.main.fieldOfView, nextFov, ref dampVelocity2, FovSmoothSpeed, Mathf.Infinity, Time.deltaTime);
        WeapCameraObj.fieldOfView = Camera.main.fieldOfView-20;

		newPosX = Mathf.SmoothDamp(gun.localPosition.x, nextPosX, ref dampVelocity, 0.09f, Mathf.Infinity, Time.deltaTime);
		newPosY = Mathf.SmoothDamp(gun.localPosition.y, nextPosY, ref dampVelocity3, 0.09f, Mathf.Infinity, Time.deltaTime);
        tempGunPos.x = newPosX;
        tempGunPos.y = newPosY;
        tempGunPos.z = gun.localPosition.z;
        gun.localPosition = tempGunPos;

		float horizontal = FPSWalker.inputX;
		float vertical = FPSWalker.inputY;
				
		if (FPSWalker.sprintActive)
        {				
			nextFov = defaultFov;	
			weaponUnzoomXPositionSprintAmt = Mathf.MoveTowards(weaponUnzoomXPositionSprintAmt, WeaponBehaviorComponent.weaponUnzoomXPositionSprint, Time.deltaTime * 16);									
		}
        else
        {
			weaponUnzoomXPositionSprintAmt = Mathf.MoveTowards(weaponUnzoomXPositionSprintAmt, 0, Time.deltaTime * 16);
		}

        nextFov = defaultFov;	
				  	
		if(!WeaponBehaviorComponent.PistolSprintAnim || !FPSWalker.canRun){
				gunAnimTime = gunObj.animation["RifleSprinting"].normalizedTime;
		}else{
				gunAnimTime = gunObj.animation["PistolSprinting"].normalizedTime;	
		}
		   
		
		if(FPSPlayerComponent.zoomed)
        {
                //右键瞄准
				nextFov = WeaponBehaviorComponent.zoomFOV;
				FovSmoothSpeed = 0.09f;

				nextPosX = WeaponBehaviorComponent.weaponZoomXPosition;  //右键瞄准时,移动一下枪的正常位置

                nextPosY = WeaponBehaviorComponent.weaponZoomYPosition;  //右键瞄准时,移动一下枪的正常位置

				FPSWalker.zoomSpeed = true;

				if(zoomSfxState && WeaponBehaviorComponent.meleeSwingDelay == 0 && !WeaponBehaviorComponent.unarmed)
                {
					AudioSource.PlayClipAtPoint(sightsUpSnd, mainCamTransform.position);
					zoomSfxState = false;
				}			
		}
        else
        {
		        //取消瞄准
				FovSmoothSpeed = 0.18f;		


                nextPosX = WeaponBehaviorComponent.weaponUnzoomXPosition  //取消瞄准时 ,恢复枪的位置为正常位置
                    + weaponUnzoomXPositionSprintAmt; //加速时,调整一下枪的位置
				nextPosY =  WeaponBehaviorComponent.weaponUnzoomYPosition
				+ (VerticalBob.translateChange/18); //这一行负责枪在移动过程中的上下抖动
				
				FPSWalker.zoomSpeed = false;	
				if(!zoomSfxState && WeaponBehaviorComponent.meleeSwingDelay == 0 && !WeaponBehaviorComponent.unarmed)
                {
					AudioSource.PlayClipAtPoint(sightsDownSnd, mainCamTransform.position);
					zoomSfxState = true;
				}
	    }		
	}	
}

 




 武器位置涉及到三个点:

1 正常时武器位置

2 瞄准时武器的位置

3 加速跑时,武器因为旋转,适当的调整一下枪的位置

 

FR:海涛高软(Hunk Xu)
QQ技术交流群:386476712

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值