01-Unity下的Input类属性
属性
- 常用属性
获取鼠标屏幕位置
Vector3 MousePos = Input.MousePosition;
屏幕触摸手机位置状态:
FingerId : 手指编号,整型
Phase:手指的阶段,枚举类型。分为几个阶段:Begin:开始接触屏幕; Moved:移动Stationary : 静止;Ended:手指离开屏幕;Canceled:系统关闭触控
Position : 手指触碰屏幕的位置,Vector2类型。坐标以屏幕左下角为原点1像素对应一个位置
// 遍历输出所有的Touch
Foreach(Touch touch in Input.touches)
{
GUILayout.Lable(string.Format(“手指:{0}状态:{1}位置:{2}”,touch.fingerId,touch.phase.ToString(),touch.position));
}
关于重力感应:
Acceleration,即加速度传感或者重力感应。可以通过Input.acceleration得到重力感应的值Vector3,值域[-1,1]
其他API:
Input.deviceorientation 得到当前游戏对运行的朝向
Input.touchSupported 得到当前游戏是否支持手指触控操作
Input.mutiTouchEnable :可以设置游戏是否支持多点触控。
02-Unity下的Input类方法
圣典翻译
代码语言:javascript复制 h = Input.GetAxis("Horizontal");
v = Input.GetAxis("Vertical");
利用虚拟轴进行移动
PlayerA.transform.Translate(h * 10f * Time.deltaTime, 0, v * 10f * Time.deltaTime;);
03-Input虚拟轴的设置
虚拟轴的设置
04-Input类的封装
代码语言:javascript复制void Update () {
h1 = Input.GetAxis("Horizontal1");
v1 = Input.GetAxis("Vertical1");
MoveObjRun(PlayerB,h1,v1);
}
// 箭头控制移动方法
private void MoveObjRun(GameObject player,float v1,float h1)
{
IfRange(KeyCode.UpArrow, player,new Vector3(0, 0, v1));
IfRange(KeyCode.DownArrow, player, new Vector3(0, 0, v1));
IfRange(KeyCode.LeftArrow, player, new Vector3(h1, 0, 0));
IfRange(KeyCode.RightArrow, player, new Vector3(h1, 0, 0));
}
peivate void IfRange(KeyCode c,GameObject player,Vector3 v3)
{
if (Input.GetKey(c))
{
TransLateObj(v3, player);
}
}
private void TransLateObj(Vector3 v,GameObject player)
{
player.transform.Translate(v);
}
- 利用方向键控制相机旋转
public class KeyRotation : MonoBehaviour {
public KeyCode keyDown = KeyCode.None;
[SerializeField]
private float Sensitivity = 5f;
private Vector3 targetPosition;
void Start () {
}
void Update () {
if (keyDown == KeyCode.None || Input.GetKey(keyDown) == true)
{
targetPosition = transform.forward * Input.GetAxisRaw("Vertical") * Sensitivity * Time.deltaTime;
targetPosition = transform.right * Input.GetAxisRaw("Horizontal") * Sensitivity * Time.deltaTime;
}
this.transform.SetPositionAndRotation(Vector3.Lerp(this.transform.position,targetPosition,Sensitivity* Time.deltaTime), Quaternion.identity);
}
}
- 按键操作相机旋转
public class KeyboardMove : MonoBehaviour {
public KeyCode Require = KeyCode.None;
public float Sensitivity = 3.0f;
public float Dampening = 8.0f;
private Vector3 targetPosition;
void Start () {
targetPosition = transform.position;
}
// Update is called once per frame
void Update () {
if (Require == KeyCode.None || Input.GetKey(Require) == true)
{
targetPosition = transform.forward * Input.GetAxisRaw("Vertical") * Sensitivity * Time.deltaTime;
targetPosition = transform.right * Input.GetAxisRaw("Horizontal") * Sensitivity * Time.deltaTime;
}
var currentPosition = Dampen3(transform.position, targetPosition, Dampening, Time.deltaTime, 0.1f);
SetPosition(transform, currentPosition);
}
public static Vector3 Dampen3(Vector3 current, Vector3 target, float dampening, float elapsed, float minStep = 0.0f)
{
var factor = DampenFactor(dampening, elapsed);
var maxDelta = Mathf.Abs((target - current).magnitude) * factor minStep * elapsed;
return Vector3.MoveTowards(current, target, maxDelta);
}
public static float DampenFactor(float dampening, float elapsed)
{
return 1.0f - Mathf.Pow((float)System.Math.E, -dampening * elapsed);
}
public static void SetPosition(Transform t, float x, float y, float z)
{
SetPosition(t, new Vector3(x, y, z));
}
public static void SetPosition(Transform t, Vector3 v)
{
if (t != null)
{
#if UNITY_EDITOR
if (Application.isPlaying == false && t.position == v) return;
#endif
t.position = v;
}
}
}