新建脚本文档
要达到鼠标控制手枪的旋转
确定枪的旋转角度
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);
}
}
标签:unity3D,shootTimer,05,手枪,float,private,旋转,maxYRotation,maxXRotation
From: https://www.cnblogs.com/flyall/p/17178078.html