unity3D控制手枪的旋转05

新建脚本文档

image

要达到鼠标控制手枪的旋转

确定枪的旋转角度

image

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

public class GunManager : MonoBehaviour
{
    //1.枪旋转角度
    private float maxYRotation = 120;
    private float minYRotation = 0;
    private float maxXRotation = 60;
    private float minXRotation = 0;

    //2.枪射击时间
    private float shootTime = 1;
    private float shootTimer = 0;


    //3.更新
    private void Update()
    {
        shootTimer += Time.deltaTime;
        if(shootTimer > shootTime)
        {
            //TODO:可以射击
        }
        
        //4.旋转 获取鼠标坐标
        float xPosPrecent=Input.mousePosition.x/Screen.width;
        float yPosPrecent = Input.mousePosition.y / Screen.height;

        //5.规定旋转角度最大最小值范围
        float xAngle = -Mathf.Clamp(yPosPrecent * maxXRotation, minXRotation, maxXRotation)+15;
        float yAngle=Mathf.Clamp(xPosPrecent*maxYRotation, minYRotation, maxYRotation)-60;

        //6.赋值给枪的组件值
        transform.eulerAngles=new Vector3(xAngle, yAngle, 0);
    }

}
posted @ 2023-03-04 12:26  flyall  阅读(62)  评论(0)    收藏  举报