Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

c# - 我正在尝试制作触摸屏游戏,但在运动中挣扎(I am trying to make a touchscreen game but am struggling with movement)

Currenty I am using vectors to move my character , but when the character reaches the touch position it keeps moving forward , because the velocity has to slow down.

(当前,我正在使用矢量移动角色,但是当角色到达触摸位置时,它会继续向前移动,因为速度必须减慢。)

I want character to reach my fingers position ( which it is doing) , but then stop immediatly.

(我希望角色到达手指位置(正在执行此操作),但要立即停止。)

Any suggestions?

(有什么建议么?)

my code:

(我的代码:)

private float? pressX;
float currentX;
public float moveSpeed = 1f;
void Update()
    {

        if (Input.GetMouseButtonDown(0))
            pressX = Input.touches[0].position.x;
        else if (Input.GetMouseButtonUp(0))
            pressX = null;
        if (pressX != null)
        {
            float currentX = Input.touches[0].position.x;
            // The finger of initial press is now left of the press position
            if (currentX < pressX)

                Move(-moveSpeed);

            // The finger of initial press is now right of the press position
            else if (currentX > pressX)

                Move(moveSpeed);

            // else is not required as if you manage (somehow)
            // move you finger back to initial X coordinate
            // you should just be staying still
        }


    void Move(float velocity)
    {
        transform.position += Vector3.right * velocity * Time.deltaTime;
    }
  ask by Marco123 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...