手枪的旋转

手枪的旋转


根据鼠标在屏幕上的相对位置旋转枪口。

首先设置一下枪的旋转角度范围。这个根据场景中具体情况选择合适的角度。

给定一个射击的时间用来控制枪的射速(这里用伪代码先写一些,主要是枪的旋转)

获取鼠标点击位置在X和Y方向上的所占比例;计算出旋转的角度。

要注意枪在上下旋转的时候实际上是绕着X轴进行旋转;枪在左右旋转的时候实际上是绕着Y轴进行旋转。

然后设置枪的欧拉角完成旋转。

public class GunManager:MonoBehaviour{

private   float maxYRotation =120;

private   float minYRotation =0;

private   float maxXRotation =60;

private   float minXRotation =0;

private float shootTime=1;

private float shootTimer=0;

private void Update()

{

        shootTimer += Time.deltaTime;

        if(shootTimer>=shootTime)

        {

            //TODO:  可以射击

         }

          float xPosPrecent = Input.mousePosition.x/ Screen.width;

          float yPosPrecent = Input.mousePosition.y/ Screen.width;

          float xAngle=-Mathf.Clamp(yPosPrecent *maxXRotation ,minXRotation, maxXRotation )+15;

          float yAngle=Mathf.Clamp(xPosPrecent*maxYRotation,minYRotation,maxYRotation)-60;

          transform.eulerAngle=new Vector3(xAngle,yAngle);

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值