Unity3d滑动跳跃反应不太灵敏

时间:2019-05-20 19:18:41

标签: c# android unity3d

我正在尝试使用unity3D构建和运行Android游戏。这是我写的角色控制器脚本。向上滑动的部分接近我评论过的代码的结尾。

float speed = 11.0f;
public float jumpForce = 0.005f;


public float FingerInitialPosition = 0f;
public float FingerMovedPosition = 0f;
public float startTouchPosition = 0f;
public float endTouchPosition = 0f;

public Rigidbody2D rb;
public Vector3 jump;
public int jumpCount;


Vector3 moveDir = Vector3.zero;

// Start is called before the first frame update
void Start()
{
    rb = GetComponent<Rigidbody2D>();
    jump = new Vector3(0.0f, 1f, 0.0f);

}


void OnCollisionEnter2D(Collision2D col)
{

    Debug.Log("Collided");
    jumpCount=0;            

}

void FixedUpdate()
{

    Debug.Log(jumpCount);
    if(transform.position.y < -14) { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); }


    foreach (Touch FingerTouch in Input.touches)
    {
        if (FingerTouch.position.x < Screen.width / 2)
        {
            if (FingerTouch.phase == TouchPhase.Began)
            {
                FingerInitialPosition = FingerTouch.position.x;
            }
            else if (FingerTouch.phase == TouchPhase.Moved)
            {
                FingerMovedPosition = FingerTouch.position.x;

                if (FingerMovedPosition > FingerInitialPosition)
                {
                     float hval = rb.velocity.x;
                     hval += 19f;
                     hval *= Mathf.Pow(0.5f, Time.deltaTime * 10f);
                     rb.velocity = new Vector2(hval, rb.velocity.y);



                }
                else if (FingerMovedPosition < FingerInitialPosition)
                {
                    float hval = rb.velocity.x;
                    hval -= 19f;
                    hval *= Mathf.Pow(0.5f, Time.deltaTime * 10f);
                    rb.velocity = new Vector2(hval, rb.velocity.y);
                }
            }
            else if (FingerTouch.phase == TouchPhase.Ended)
            {
                FingerInitialPosition = 0f;
                FingerMovedPosition = 0f;

            }
            else //NEW PART!//
            {
                FingerMovedPosition = FingerTouch.position.x;
                if (FingerMovedPosition > FingerInitialPosition)
                {
                    float hval = rb.velocity.x;
                    hval += 19f;
                    hval *= Mathf.Pow(0.5f, Time.deltaTime * 10f);
                    rb.velocity = new Vector2(hval, rb.velocity.y);
                }
                else if (FingerMovedPosition < FingerInitialPosition)
                {
                    float hval = rb.velocity.x;
                    hval -= 19f;
                    hval *= Mathf.Pow(0.5f, Time.deltaTime * 10f);
                    rb.velocity = new Vector2(hval, rb.velocity.y);

                }
            }
        }

        // The part I think needs fixing
        // The swipe Up part where I make the player jump

        if (FingerTouch.position.x > Screen.width / 2)
        {
            for (int i = 0; i < Input.touchCount; i++)
            {
                Touch touch = Input.GetTouch(i);
                if (touch.phase == TouchPhase.Began)
                {
                    startTouchPosition = touch.position.y;
                }
                else if (touch.phase == TouchPhase.Ended)
                {
                    endTouchPosition = touch.position.y;
                    if (endTouchPosition > startTouchPosition)
                    {
                        if (jumpCount<1) {
                            rb.AddForce(jump * jumpForce, ForceMode2D.Impulse);
                            jumpCount += 1;
                        }

                    }
                }
            }
        }



    }



}

在屏幕左侧滑动(水平)可以让玩家左右移动,在屏幕右侧向上滑动可以让玩家跳过我的问题,就是跳动的反应性不是很好,我有做了一个测试,它只响应了3/5次。

我还要附加播放器的屏幕截图

enter image description here

1 个答案:

答案 0 :(得分:1)

继续使用static func refreshAuthorisation() { let group = DispatchGroup() if Auth.auth().currentUser == nil { return } group.enter() Auth.auth().currentUser?.reload() { (error) in group.leave() } group.wait() } 项目,而不要使用嵌套循环。

此外,不要在手指移开时进行比较(这可能会导致延迟),而只要检查到FingerTouch就可以激活它:

FingerTouch.deltaPosition.y

这可能只是部分解决方案。如果这仍然不起作用,我很好奇是否删除了void FixedUpdate() { Debug.Log(jumpCount); if(transform.position.y < -14) { SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex); } foreach (Touch FingerTouch in Input.touches) { if (FingerTouch.position.x < Screen.width / 2) { if (FingerTouch.phase == TouchPhase.Began) { FingerInitialPosition = FingerTouch.position.x; } else if (FingerTouch.phase == TouchPhase.Moved) { FingerMovedPosition = FingerTouch.position.x; if (FingerMovedPosition > FingerInitialPosition) { float hval = rb.velocity.x; hval += 19f; hval *= Mathf.Pow(0.5f, Time.deltaTime * 10f); rb.velocity = new Vector2(hval, rb.velocity.y); } else if (FingerMovedPosition < FingerInitialPosition) { float hval = rb.velocity.x; hval -= 19f; hval *= Mathf.Pow(0.5f, Time.deltaTime * 10f); rb.velocity = new Vector2(hval, rb.velocity.y); } } else if (FingerTouch.phase == TouchPhase.Ended) { FingerInitialPosition = 0f; FingerMovedPosition = 0f; } else //NEW PART!// { FingerMovedPosition = FingerTouch.position.x; if (FingerMovedPosition > FingerInitialPosition) { float hval = rb.velocity.x; hval += 19f; hval *= Mathf.Pow(0.5f, Time.deltaTime * 10f); rb.velocity = new Vector2(hval, rb.velocity.y); } else if (FingerMovedPosition < FingerInitialPosition) { float hval = rb.velocity.x; hval -= 19f; hval *= Mathf.Pow(0.5f, Time.deltaTime * 10f); rb.velocity = new Vector2(hval, rb.velocity.y); } } } else if (FingerTouch.position.x > Screen.width / 2) { if (FingerTouch.deltaPosition.y > 0) { if (jumpCount<1) { rb.AddForce(jump * jumpForce, ForceMode2D.Impulse); jumpCount += 1; } } } } } 检查。如果确实如此,则该问题似乎与您将其设置回if (jumpCount<1)的方式有关。