gpt4 book ai didi

c# - Unity3d Swipe 跳转不是很灵敏

转载 作者:太空宇宙 更新时间:2023-11-03 12:03:10 24 4
gpt4 key购买 nike

我正在尝试使用 unity3D 构建安卓游戏。这是我编写的角色 Controller 脚本。向上滑动部分接近我评论过的代码的末尾。

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

最佳答案

不要使用嵌套循环,而是继续使用 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)检查使它工作。如果是这样,问题似乎与您如何将其设置回 0 有关。 .

关于c# - Unity3d Swipe 跳转不是很灵敏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56226910/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com