Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

unity3d - Moving player in Subway Surf like game using left/right swipe

I am developing endless runner game like subway surfer in Unity. I want to move my player, smoothly on swipe left or right. How can do it? Here is my code:

using UnityEngine;
using System.Collections;

public class SwipeScript3 : MonoBehaviour {

private Touch initialTouch = new Touch();
private float distance = 0;
private bool hasSwiped = false;
//Quaternion targetx = Quaternion.Euler(0, -3f, 0);
//Quaternion targety = Quaternion.Euler(0, 3f, 0);

void FixedUpdate()
{
    foreach(Touch t in Input.touches)
    {
        if (t.phase == TouchPhase.Began)
        {
            initialTouch = t;
        }
        else if (t.phase == TouchPhase.Moved && !hasSwiped)
        {
            float deltaX = initialTouch.position.x - t.position.x;
            float deltaY = initialTouch.position.y - t.position.y;
            distance = Mathf.Sqrt((deltaX * deltaX) + (deltaY * deltaY));
            bool swipedSideways = Mathf.Abs(deltaX) > Mathf.Abs(deltaY);

            if (distance > 50f)
            {
                if (swipedSideways && deltaX > 0) //swiped left
                {

                    //transform.rotation = Quaternion.Slerp (transform.rotation, targetx, Time.deltaTime * 0.8f);
                    this.transform.Rotate(new Vector3(0, -3f, 0)*Time.deltaTime);
                    //transform.position = Vector3.Lerp (transform.position,new Vector3(transform.position.x+5f,transform.position.y,transform.position.z),Time.deltaTime*2f );
                    transform.position = Vector3.Lerp (transform.position, new Vector3 (transform.position.x - 5f, transform.position.y, transform.position.z), Time.deltaTime * 5f);
                }
                else if (swipedSideways && deltaX <= 0) //swiped right
                {
                    //transform.rotation = Quaternion.Slerp (transform.rotation, targety, Time.deltaTime * 0.8f);
                    this.transform.Rotate(new Vector3(0, 3f, 0)*Time.deltaTime);
                    //transform.position = Vector3.Lerp (transform.position, new Vector3 (transform.position.x - 5f, transform.position.y, transform.position.z), Time.deltaTime * f);
                    transform.position = Vector3.Lerp (transform.position,new Vector3(transform.position.x+5f,transform.position.y,transform.position.z),Time.deltaTime*5f );
                }
                else if (!swipedSideways && deltaY > 0) //swiped down
                {
                    //this.transform.Rotate(new Vector3(0, 2f, 0));
                    transform.position = Vector3.Lerp (transform.position,new Vector3(transform.position.x,transform.position.y,transform.position.z-5f),Time.deltaTime*2f );
                }
                else if (!swipedSideways && deltaY <= 0)  //swiped up
                {
                    this.GetComponent<Rigidbody>().velocity = new Vector3(this.GetComponent<Rigidbody>().velocity.x, 0, this.GetComponent<Rigidbody>().velocity.z);
                    this.GetComponent<Rigidbody>().AddForce(new Vector3(0, 400f, 0));
                    Debug.Log ("Swiped Up");
                }

                hasSwiped = true;
            }

        }
        else if (t.phase == TouchPhase.Ended)
        {
            initialTouch = new Touch();
            hasSwiped = false;
        }
    }
}
}
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You could use Vector3.Slerp(Vector3 StartPosition, Vector3 DestinationPosition, float Number)

  • Number is between 0 and 1 and it indicates where will be the position of your object between StartPosition and DestinationPosition.

  • Lets say Number = 0.0f;: your object will be at StartPosition.

  • If Number = 0.5f;: your object will be between StartPosition and DestinationPosition.

  • You need to increase the Number value from 0 to 1 when swipe action is performed.

  • The faster you increase the "Number" value, the faster your object will move towards Destination.

  • You should set you StartPosition once when the swipe action begins, not give your transform.position repeatedly in your Vector3.Slerp() function.

You can find an example here in Unity Docs.

Hope this helps! Cheers!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...