unity手游触摸动作不稳固 [英] Unity mobile game touch movement not solid
本文介绍了unity手游触摸动作不稳固的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一种错误"在我的代码中,我只是无法找到它发生的原因以及如何解决它(我是 Unity 的初学者,甚至更喜欢 Unity 的手机游戏)
I have kind of a "bug" in my code that I just can't find why its happening and how to fix it (I'm a beginner in unity and even more in mobile games in unity )
我使用触摸让玩家从一侧移动到另一侧,但问题是我希望玩家在左右滑动手指时平滑移动,但我的代码也会将玩家移动到您点击的位置,而不仅仅是在您点击时幻灯片.
I have player movement from side to side using the touch but the problem is I want the player to move smoothly when sliding the finger from side to side but the code I have also moves the player where you tap and not only when you slide.
任何帮助将不胜感激,谢谢大家:)
Any help would be appreciated, thank you all :)
我的代码:
public float playerspeed = 500f;
public float directionalspeed;
void Start()
{
}
void Update()
{
float movehorizontal = Input.GetAxis("Horizontal");
transform.position = Vector3.Lerp(gameObject.transform.position, new Vector3(Mathf.Clamp(gameObject.transform.position.x + movehorizontal, -1f, 1f), gameObject.transform.position.y, gameObject.transform.position.z), directionalspeed * Time.deltaTime);
// -------------------------MOBILE CONTROLS SECTION STARTS HERE ------------------------------------------------
GetComponent<Rigidbody>().velocity = Vector3.forward * playerspeed * Time.deltaTime;
//collects the postion of the finger on the screen
Vector2 touch = Camera.main.ScreenToWorldPoint(Input.mousePosition + new Vector3(0, 0, 10f));
//if there are more then 0 fingers on the screen , move the ball smoothly on the X axis to where the finger is pointing
if (Input.touchCount > 0)
{
transform.position = new Vector3(touch.x, transform.position.y, transform.position.z);
}
}
推荐答案
试试这个:
void Update()
{
//other code
if (Input.touchCount > 0)
{
//you might want to lower playerSpeed
if (transform.position.x > touch.x)
{
transform.Translate(Vector3.left * playerSpeed * Time.deltaTime);
}
else if (transform.position.x < touch.x)
{
transform.Translate(Vector3.right * playerSpeed * Time.deltaTime);
}
}
}
这篇关于unity手游触摸动作不稳固的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文