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
93 views
in Technique[技术] by (71.8m points)

c# - Getting this to work on android control touch screen?

Getting this to work on android control touch screen ? In Unity I am rly bad at unity and programming as i just started using em so I just want some help :)

using UnityEngine;


public class PlayerMovement : MonoBehaviour {

    // This is a reference to the Rigidbody component called "rb"
    public Rigidbody rb;

    public float forwardForce = 2000f;  // Variable that determines the forward force
    public float sidewaysForce = 500f;  // Variable that determines the sideways force

    // We marked this as "Fixed"Update because we
    // are using it to mess with physics.
    void FixedUpdate ()
    {
        // Add a forward force
        rb.AddForce(0, 0, forwardForce * Time.deltaTime);

        if (Input.GetKey("d"))  // If the player is pressing the "d" key
        {
            // Add a force to the right
            rb.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }

        if (Input.GetKey("a"))  // If the player is pressing the "a" key
        {
            // Add a force to the left
            rb.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
        }

        if (rb.position.y < -1f)
        {
            FindObjectOfType<GameManager>().EndGame();
        }
    }
}
question from:https://stackoverflow.com/questions/65871847/getting-this-to-work-on-android-control-touch-screen

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

1 Answer

0 votes
by (71.8m points)

if you want to allow player to control it with swipes you can use this code :

enter code here




 private Vector2 fingerDownPosition;
 private Vector2 fingerUpPosition;

[SerializeField]
private bool detectSwipeOnlyAfterRelease = true;

[SerializeField]
private float minDistanceForSwipe = 20f;

public static event Action<SwipeData> OnSwipe = delegate { };

private void Update()
{
    foreach (Touch touch in Input.touches)
    {
        if (touch.phase == TouchPhase.Began)
        {
            fingerUpPosition = touch.position;
            fingerDownPosition = touch.position;
        }

        if (!detectSwipeOnlyAfterRelease && touch.phase == TouchPhase.Moved)
        {
            fingerDownPosition = touch.position;
            DetectSwipe();
        }

        if (touch.phase == TouchPhase.Ended)
        {
            fingerDownPosition = touch.position;
            DetectSwipe();
        }
    }
}

private void DetectSwipe()
{
    if (SwipeDistanceCheckMet())
    {
        if (IsVerticalSwipe())
        {
            var direction = fingerDownPosition.y - fingerUpPosition.y > 0 ? SwipeDirection.Up : SwipeDirection.Down;
            SendSwipe(direction);
        }
        else
        {
            var direction = fingerDownPosition.x - fingerUpPosition.x > 0 ? SwipeDirection.Right : SwipeDirection.Left;
            SendSwipe(direction);
        }
        fingerUpPosition = fingerDownPosition;
    }
}

private bool IsVerticalSwipe()
{
    return VerticalMovementDistance() > HorizontalMovementDistance();
}

private bool SwipeDistanceCheckMet()
{
    return VerticalMovementDistance() > minDistanceForSwipe || HorizontalMovementDistance() > minDistanceForSwipe;
}

private float VerticalMovementDistance()
{
    return Mathf.Abs(fingerDownPosition.y - fingerUpPosition.y);
}

private float HorizontalMovementDistance()
{
    return Mathf.Abs(fingerDownPosition.x - fingerUpPosition.x);
}

private void SendSwipe(SwipeDirection direction)
{
    SwipeData swipeData = new SwipeData()
    {
        Direction = direction,
        StartPosition = fingerDownPosition,
        EndPosition = fingerUpPosition
    };

    OnSwipe(swipeData);

    }}public struct SwipeData{ public Vector2 StartPosition;public Vector2 EndPosition;
public SwipeDirection Direction;

}

public enum SwipeDirection { Up, Down, Left, Right }


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

...