1 using System;
2 using
UnityEngine;
3 using
UnityStandardAssets.CrossPlatformInput;
4
5 namespace
UnityStandardAssets.Vehicles.Aeroplane
6 {
7     
[RequireComponent(typeof (AeroplaneController))]
8     
public class AeroplaneUserControl2Axis : MonoBehaviour
9     {
10         
// these max angles are only used on mobile, due to the way pitch and roll input are handled
11         
public float maxRollAngle = 80;
12         
public float maxPitchAngle = 80;
13
14         
// reference to the aeroplane that we're controlling
15         
private AeroplaneController m_Aeroplane;
16
17
18         
private void Awake()
19         {
20             
// Set up the reference to the aeroplane controller.
21             m_Aeroplane = GetComponent<AeroplaneController>();
22         }
23
24
25         
private void FixedUpdate()
26         {
27             
// Read input for the pitch, yaw, roll and throttle of the aeroplane.
28             
float roll = CrossPlatformInputManager.GetAxis("Horizontal");
29             
float pitch = CrossPlatformInputManager.GetAxis("Vertical");
30             
bool airBrakes = CrossPlatformInputManager.GetButton("Fire1");
31
32             
// auto throttle up, or down if braking.
33             
float throttle = airBrakes ? -1 : 1;
34 #
if MOBILE_INPUT
35             AdjustInputForMobileControls(
ref roll, ref pitch, ref throttle);
36 #endif
37             
// Pass the input to the aeroplane
38             m_Aeroplane.Move(roll, pitch,
0, throttle, airBrakes);
39         }
40
41
42         
private void AdjustInputForMobileControls(ref float roll, ref float pitch, ref float throttle)
43         {
44             
// because mobile tilt is used for roll and pitch, we help out by
45             
// assuming that a centered level device means the user
46             
// wants to fly straight and level!
47
48             
// this means on mobile, the input represents the *desired* roll angle of the aeroplane,
49             
// and the roll input is calculated to achieve that.
50             
// whereas on non-mobile, the input directly controls the roll of the aeroplane.
51
52             
float intendedRollAngle = roll*maxRollAngle*Mathf.Deg2Rad;
53             
float intendedPitchAngle = pitch*maxPitchAngle*Mathf.Deg2Rad;
54             roll = Mathf.Clamp((intendedRollAngle - m_Aeroplane.RollAngle), -
1, 1);
55             pitch = Mathf.Clamp((intendedPitchAngle - m_Aeroplane.PitchAngle), -
1, 1);
56
57             
// similarly, the throttle axis input is considered to be the desired absolute value, not a relative change to current throttle.
58             
float intendedThrottle = throttle*0.5f + 0.5f;
59             throttle = Mathf.Clamp(intendedThrottle - m_Aeroplane.Throttle, -
1, 1);
60         }
61     }
62 }


Tìm kết quả nhanh