手枪的旋转
根据鼠标在屏幕上的相对位置旋转枪口。
首先设置一下枪的旋转角度范围。这个根据场景中具体情况选择合适的角度。
给定一个射击的时间用来控制枪的射速(这里用伪代码先写一些,主要是枪的旋转)
获取鼠标点击位置在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);
}
}