统一运动触摸 [英] Unity Movement Touch

查看:177
本文介绍了统一运动触摸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一个足球点球如游戏,我需要移动球移动了。现在,我使用这个代码:

I'm making a football-penalty like game and I need to move the ball on mobile too. I'm using this code now:

float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rigidbody.AddForce(movement * speed * Time.deltaTime);



我怎样才能使运动系统,移动?

How can i make a movement System For mobile?

此外,还有就是控制球(当你通过一条线(就像保龄球一样),你只能改变它的位置1/10)someother代码。但他们都是基于这个代码...游戏脚本是C#...

Also, there is someother codes that control the ball (like when you pass a line (just like bowling) you can only change it's place 1/10). But they are based on this code... Game script is c#...

推荐答案

使用轴触摸开关,这将有点困难。我建议阅读有关Touchscript( http://interactivelab.github.io/TouchScript/ )以及如何API的工作一般。你会需要做的就是当玩家触摸屏幕,你缓存联系,当他们碰你直呼其触摸比较其当前位置测量运动的方向。 。通过这样做,你可以刷卡路线

Switching this from using axis to touch will be a bit difficult. I recommend reading about Touchscript (http://interactivelab.github.io/TouchScript/) and how the API works in general. What you're going to need to do is when the player touches the screen, you cache that touch and while they're touching you measure the direction of the movement by comparing their first touch to their current position. By doing this you can get swipe directions.

像这样的东西可以让你开始:

Something like this could get you started:

public void OnTouchBegin( object sender, TouchEventArgs e ){
    foreach( TouchPoint point in e.TouchPoints ){
        firstTouchPosition = new Vector2( point.Position.x, point.Position.y );
    }
}

public void OnTouchContinue( object sender, TouchEventArgs e ){
    foreach( var point in e.TouchPoints ){
        secondTouchPosition  = new Vector2( point.Position.x, point.Position.y );
        currentTouchMovement = new Vector2( ( secondTouchPosition.x - firstTouchPosition.x ),
        ( secondTouchPosition.y - firstTouchPosition.y ));
        currentTouchMovement.Normalize( );
    }
}

public void OnTouchEnd( object sender, TouchEventArgs e ){
    hasTouchEnded = true;
}

和你需要一些自定义代码来确定X和Y轴方向与触摸输入相对应。另外要小心屏幕坐标和游戏中的坐标为它们之间的区别相当多。这看起来令人生畏在第一,但也有一些工作,我保证它不是太糟糕了!希望这有助于!

And you'll need some custom code to determine the X and Y axis direction corresponds with the touch input. Also be careful about screen coordinates and in-game coordinates as they differ quite a bit. This looks daunting at first, but with some work I promise its not too bad! Hope this helps!

这篇关于统一运动触摸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆