Input Unity3d模拟GetAxis
我试图根据触摸屏幕的位置模拟Input.GetAxis(“水平”)和Input.GetAxis(“垂直”)的加速度。因此,假设我在屏幕中间有一个对象叫做“MealLyObjor”。我想做的是,如果我触摸对象的右侧,模拟输入从0逐渐增加到1。GetAxis(“水平”)会这样做,然后如果我将手指放在左侧,它会迅速返回到0,然后逐渐减少到-1。基本上将input.getaxis转换为触摸友好的版本。你知道我该怎么做吗Input Unity3d模拟GetAxis,input,unity3d,touch,Input,Unity3d,Touch,我试图根据触摸屏幕的位置模拟Input.GetAxis(“水平”)和Input.GetAxis(“垂直”)的加速度。因此,假设我在屏幕中间有一个对象叫做“MealLyObjor”。我想做的是,如果我触摸对象的右侧,模拟输入从0逐渐增加到1。GetAxis(“水平”)会这样做,然后如果我将手指放在左侧,它会迅速返回到0,然后逐渐减少到-1。基本上将input.getaxis转换为触摸友好的版本。你知道我该怎么做吗 谢谢 听起来你需要Lerp的魔力:)(线性插值) 基本上,您需要确定您是否位于参考点
谢谢 听起来你需要Lerp的魔力:)(线性插值) 基本上,您需要确定您是否位于参考点的左侧或右侧(或向上或向下)。在这种情况下,让我们假设屏幕的中心。如果是,则相应地向1或-1移动 在触摸屏上,这意味着除非您添加“死区”,否则您将永远不会处于零位,这是一种痛苦,因此您还应检查距离中心是否太小而无暇顾及 然后,您可以使用Lerp功能以您选择的速度从您现在的位置移动到您想要的位置 下面是一些带有注释的代码,向您展示我是如何做到这一点的
// for this example, using the actual screen centre as our central point
// change this if you like :)
Vector2 myCentre = new Vector2( Screen.width/2, Screen.height/2 );
Vector2 touchPos = new Vector2( Screen.width/2, Screen.height/2 );
// change this to affect how quickly the number moves toward its destination
float lerpSpeed = 0.3f;
// set this as big or small as you want. I'm using a factor of the screen's size
float deadZone = 0.1f * Mathf.Min( Screen.width, Screen.height );
public void Update()
{
Vector2 delta = (Vector2)Input.mousePosition - myCentre;
// if the mouse is down / a touch is active...
if( Input.GetMouseButton( 0 ) == true )
{
// for the X axis...
if( delta.x > deadZone )
{
// if we're to the right of centre and out of the deadzone, move toward 1
touchPos.x = Mathf.Lerp( touchPos.x, 1, lerpSpeed );
}
else if( delta.x < -deadZone )
{
// if we're to the left of centre and out of the deadzone, move toward -1
touchPos.x = Mathf.Lerp( touchPos.x, -1, lerpSpeed );
}
else
{
// otherwise, we're in the deadzone, move toward 0
touchPos.x = Mathf.Lerp( touchPos.x, 0, lerpSpeed );
}
// repeat for the Y axis...
if( delta.y > deadZone )
{
touchPos.y = Mathf.Lerp( touchPos.y, 1, lerpSpeed );
}
else if( delta.y < -deadZone )
{
touchPos.y = Mathf.Lerp( touchPos.y, -1, lerpSpeed );
}
else
{
touchPos.y = Mathf.Lerp( touchPos.y, 0, lerpSpeed );
}
}
else
{
// the mouse is up / no touch recorded, so move both axes toward 0
touchPos.x = Mathf.Lerp( touchPos.x, 0, lerpSpeed );
touchPos.y = Mathf.Lerp( touchPos.y, 0, lerpSpeed );
}
Debug.Log("TouchPos: " + touchPos );
}
//在本例中,使用实际屏幕中心作为中心点
//如果愿意,请更改此选项:)
Vector2 myCentre=新的Vector2(Screen.width/2,Screen.height/2);
Vector2 touchPos=新的Vector2(Screen.width/2,Screen.height/2);
//更改此选项可影响数字向其目标移动的速度
浮子速度=0.3f;
//根据需要将其设置为大或小。我使用的是屏幕大小的一个因素
浮动死区=0.1f*Mathf.Min(屏幕宽度、屏幕高度);
公共无效更新()
{
Vector2 delta=(Vector2)Input.mousePosition-myCentre;
//如果鼠标按下/触摸处于活动状态。。。
if(Input.GetMouseButton(0)==true)
{
//对于X轴。。。
如果(增量x>死区)
{
//如果我们在中间偏右,离开死区,向1移动
touchPos.x=Mathf.Lerp(touchPos.x,1,lerpSpeed);
}
否则如果(增量x<-死区)
{
//如果我们在中间偏左,离开死区,向-1移动
touchPos.x=Mathf.Lerp(touchPos.x,-1,lerpSpeed);
}
其他的
{
//否则,我们处于死区,向0移动
touchPos.x=Mathf.Lerp(touchPos.x,0,lerpSpeed);
}
//对Y轴重复此操作。。。
如果(增量y>死区)
{
touchPos.y=Mathf.Lerp(touchPos.y,1,lerpSpeed);
}
否则如果(增量y<-死区)
{
touchPos.y=Mathf.Lerp(touchPos.y,-1,lerpSpeed);
}
其他的
{
touchPos.y=Mathf.Lerp(touchPos.y,0,lerpSpeed);
}
}
其他的
{
//鼠标向上/未记录触摸,因此将两个轴移向0
touchPos.x=Mathf.Lerp(touchPos.x,0,lerpSpeed);
touchPos.y=Mathf.Lerp(touchPos.y,0,lerpSpeed);
}
Debug.Log(“TouchPos:+TouchPos”);
}