gpt4 book ai didi

c# - unity 2D - 如何通过在触摸屏上向上拖动手指来跳跃?

转载 作者:太空宇宙 更新时间:2023-11-03 15:42:43 25 4
gpt4 key购买 nike

首先,抱歉我的英语不好。我正在制作一款基于 unity 2D 引擎并使用 C# 语言的游戏......我的触摸系统有问题,我无法解决它。现在我有一个跳跃键,但这不是我想要的跳跃方式.我只想让我的播放器在手指触摸屏幕并向上拖动时跳起来。这是我的代码:

// GUI textures
public GUITexture guiLeft;
public GUITexture guiRight;
public GUITexture guiJump;

// Movement variables
public float moveSpeed = 5f;
public float jumpForce = 50f;
public float maxJumpVelocity = 2f;

// Movement flags
private bool moveLeft, moveRight, doJump = false;

// Update is called once per frame
void Update () {

// Check to see if the screen is being touched
if (Input.touchCount > 0)
{
// Get the touch info
Touch t = Input.GetTouch(0);

// Did the touch action just begin?
if (t.phase == TouchPhase.Began)
{
// Are we touching the left arrow?
if (guiLeft.HitTest(t.position, Camera.main))
{
Debug.Log("Touching Left Control");
moveLeft = true;
}

// Are we touching the right arrow?
if (guiRight.HitTest(t.position, Camera.main))
{
Debug.Log("Touching Right Control");
moveRight = true;
}

// Are we touching the jump button?
if (guiJump.HitTest(t.position, Camera.main))
{
Debug.Log("Touching Jump Control");
doJump = true;
}
}

// Did the touch end?
if (t.phase == TouchPhase.Ended)
{
// Stop all movement
doJump = moveLeft = moveRight = false;
rigidbody2D.velocity = Vector2.zero;
}
}

// Is the left mouse button down?
if (Input.GetMouseButtonDown(0))
{
// Are we clicking the left arrow?
if (guiLeft.HitTest(Input.mousePosition, Camera.main))
{
Debug.Log("Touching Left Control");
moveLeft = true;
}

// Are we clicking the right arrow?
if (guiRight.HitTest(Input.mousePosition, Camera.main))
{
Debug.Log("Touching Right Control");
moveRight = true;
}

// Are we clicking the jump button?
if (guiJump.HitTest(Input.mousePosition, Camera.main))
{
Debug.Log("Touching Jump Control");
doJump = true;
}
}

if (Input.GetMouseButtonUp(0))
{
// Stop all movement on left mouse button up
doJump = moveLeft = moveRight = false;
rigidbody2D.velocity = Vector2.zero;
}
}

void FixedUpdate()
{
// Set velocity based on our movement flags.
if (moveLeft)
{
rigidbody2D.velocity = -Vector2.right * moveSpeed;
}

if (moveRight)
{
rigidbody2D.velocity = Vector2.right * moveSpeed;
}

if (doJump)
{
// If we have not reached the maximum jump velocity, keep applying force.
if (rigidbody2D.velocity.y < maxJumpVelocity)
{
rigidbody2D.AddForce(Vector2.up * jumpForce);
} else {
// Otherwise stop jumping
doJump = false;
}
}
}

最佳答案

使用

Input.GetTouch(0).deltaPosition.

它会在每次更新时给出您移动手的方向。

关于c# - unity 2D - 如何通过在触摸屏上向上拖动手指来跳跃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29648589/

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