【问题标题】:Unity mobile game touch movement not solidunity手游触控动作不扎实
【发布时间】:2020-07-28 23:28:39
【问题描述】:

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

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

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

我的代码:

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

}

【问题讨论】:

    标签: c# unity3d mobile


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-04
      相关资源
      最近更新 更多