这个问题解决了,但是发现Input.ResetInputAxes()会导致物体停顿一段时间,但是API手册上写的是停顿一帧,不知道这个函数是怎样的工作原理
代码如下
using UnityEngine;
using System.Collections;
public class ButtonEvent : MonoBehaviour {
public float Velocity = 10;
private float v = 0;
private float h = 0;
private bool isResetV = false;
private bool isResetH = false;
public float V
{
set
{
if (value != this.v)
{
if (isResetH == false)
{
Input.ResetInputAxes();
isResetH = true;
}
this.v = value;
}
}
get
{
return this.v;
}
}
public float H
{
set
{
if (value != this.h)
{
if (isResetV == false)
{
Input.ResetInputAxes();
isResetV = true;
}
this.h = value;
}
}
get
{
return this.h;
}
}
Vector3 direction;
void Update () {
if (Input.anyKeyDown)
{
isResetH = false;
isResetV = false;
}
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D) )
{
isResetH = false;
isResetV = false;
}
V = Input.GetAxisRaw("Vertical");
H = Input.GetAxisRaw("Horizontal");
direction = new Vector3(v, 0, h);
transform.Translate(direction * Time.deltaTime * Velocity);
v = 0;
h = 0;
}
}
代码如下
using UnityEngine;
using System.Collections;
public class ButtonEvent : MonoBehaviour {
public float Velocity = 10;
private float v = 0;
private float h = 0;
private bool isResetV = false;
private bool isResetH = false;
public float V
{
set
{
if (value != this.v)
{
if (isResetH == false)
{
Input.ResetInputAxes();
isResetH = true;
}
this.v = value;
}
}
get
{
return this.v;
}
}
public float H
{
set
{
if (value != this.h)
{
if (isResetV == false)
{
Input.ResetInputAxes();
isResetV = true;
}
this.h = value;
}
}
get
{
return this.h;
}
}
Vector3 direction;
void Update () {
if (Input.anyKeyDown)
{
isResetH = false;
isResetV = false;
}
if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.D) )
{
isResetH = false;
isResetV = false;
}
V = Input.GetAxisRaw("Vertical");
H = Input.GetAxisRaw("Horizontal");
direction = new Vector3(v, 0, h);
transform.Translate(direction * Time.deltaTime * Velocity);
v = 0;
h = 0;
}
}