VR飞机项目2016/9/19-25获取按键输入(虚拟按键等)

来源:互联网 发布:模拟人生4捏帅哥数据 编辑:程序博客网 时间:2024/06/05 02:03

     通过虚拟摇杆控制飞机的左右上下,这次没有使用控制rotation上的xyz3个轴的值来实现,而是通过每帧增加一个增量来实现

mainRot = this.transform.rotation;.AddRot.eulerAngles = new Vector3 (pitch, yaw, -roll);mainRot *= AddRot;transform.rotation = Quaternion.Lerp (transform.rotation, mainRot, Time.fixedDeltaTime * RotationSpeed);

<span style="white-space:pre"></span>public void AxisControl (Vector2 axis){if (SimpleControl) {LimitAxisControl.y = LimitAxisControl.x;}roll = Mathf.Lerp (roll, Mathf.Clamp (axis.x, -LimitAxisControl.x, LimitAxisControl.x) * SpeedRoll, Time.deltaTime);pitch = Mathf.Lerp (pitch, Mathf.Clamp (axis.y, -LimitAxisControl.y, LimitAxisControl.y) * SpeedPitch, Time.deltaTime);}
<span style="white-space:pre"></span>public void TurnControl (float turn){yaw += turn * Time.deltaTime * SpeedYaw;}
flight.AxisControl (new Vector2 (Input.GetAxis ("Mouse X"), Input.GetAxis ("Mouse Y")));flight.TurnControl(Input.GetAxis("VirtualAxisX"));flight.TurnControl (Input.GetAxis ("Horizontal"));


如果想要固定某个轴,不让在这个轴上旋转,可以以下代码实现,实现固定某个轴

if (SimpleControl) {  Quaternion saveQ = mainRot;    <span style="color:#009900;">//先用saveQ记录下来这一帧的Quaternion //再记录下这一帧的angle</span>  Vector3 fixedAngles = new Vector3 (mainRot.eulerAngles.x, mainRot.eulerAngles.y, mainRot.eulerAngles.z);  if(FixedX)   <span style="color:#009900;">//bool值,如果固定X轴,就让angle对应的值为1</span>  fixedAngles.x = 1;  if(FixedY)  fixedAngles.y = 1;  if(FixedZ)  fixedAngles.z = 1; <span style="color:#009900;">//算出固定轴以后的Quaternion,然后让物体从现在的Quaternion旋转到固定某个轴以后的Quaternion</span>  saveQ.eulerAngles = fixedAngles;  mainRot = Quaternion.Lerp (mainRot, saveQ, Time.fixedDeltaTime * 2);  }


获取虚拟按键的方法

public void DetectPressedKeyOrButton()    {        foreach (KeyCode kcode in Enum.GetValues(typeof(KeyCode)))        {            if (Input.GetKeyDown(kcode))            {                Debug.Log("KeyCode down: " + kcode);            }        }    }

使用了刚体的子弹,想让子弹朝目标有偏差的设计,可以用以下方法

Vector3spread = newVector3(Random.Range(-Spread, Spread), Random.Range(-Spread, Spread), Random.Range(-Spread, Spread)) / 100;Vector3 direction = this.transform.forward + spread;GameObject bullet = (GameObject)Instantiate(Missile, missileposition, missilerotate);bullet.transform.forward = direction;





0 0
原创粉丝点击