繁体   English   中英

unity手游触控动作不扎实

[英]Unity mobile game touch movement not solid

我的代码中有一种“错误”,我只是找不到它发生的原因以及如何修复它(我是统一的初学者,甚至更多的是统一的手机游戏)

我使用触摸使玩家从一侧移动到另一侧,但问题是我希望玩家在手指从一侧滑动到另一侧时平滑移动,但我的代码也将播放器移动到您点击的位置,而不仅仅是在您滑动时。

任何帮助将不胜感激,谢谢大家:)

我的代码:

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);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM