Touch Controls unity 2D
Asked Answered
W

2

6

I have script called PlayerCharacter to control a player on the Unity 2D Platform. It's perfect, working as usual.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[RequireComponent(typeof (Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
public class PlayerCharacter : MonoBehaviour
{
    public float speed = 1.0f;
    public string axisName = "Horizontal";
    private Animator anim;
    public string jumpButton = "Fire1";
    public float jumpPower = 10.0f;
    public float minJumpDelay = 0.5f;
    public Transform[] groundChecks;

    private float jumpTime = 0.0f;
    private Transform currentPlatform = null;
    private Vector3 lastPlatformPosition = Vector3.zero;
    private Vector3 currentPlatformDelta = Vector3.zero;


    // Use this for initialization
    void Start ()
    {
        anim = gameObject.GetComponent<Animator>();
    }


    // Update is called once per frame
    void Update ()
    {
        //Left and right movement
        anim.SetFloat("Speed", Mathf.Abs(Input.GetAxis(axisName)));
        if(Input.GetAxis(axisName) < 0)
        {
        Vector3 newScale = transform.localScale;
        newScale.x = -1.0f;
        transform.localScale = newScale;
        Debug.Log("Move to left");
        }
        else if(Input.GetAxis(axisName) > 0)
        {
        Vector3 newScale = transform.localScale;
        newScale.x = 1.0f;
        transform.localScale = newScale;
        Debug.Log ("Move to Right");
        }
        transform.position += transform.right*Input.GetAxis(axisName)*speed*Time.deltaTime;

        //Jump logic
        bool grounded = false;
        foreach(Transform groundCheck in groundChecks)
        {
            grounded |= Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
        }
        anim.SetBool("Grounded", grounded);
        if(jumpTime > 0)
        {
            jumpTime -= Time.deltaTime;
        }
        if(Input.GetButton("jumpButton")  && anim.GetBool("Grounded") )

        {
            anim.SetBool("Jump",true);
            rigidbody2D.AddForce(transform.up*jumpPower);
            jumpTime = minJumpDelay;
        }
        if(anim.GetBool("Grounded") && jumpTime <= 0)
        {
            anim.SetBool("Jump",false);
        }
        //Moving platform logic
        //Check what platform we are on
        List<Transform> platforms = new List<Transform>();
        bool onSamePlatform = false;
        foreach(Transform groundCheck in groundChecks)
        {
            RaycastHit2D hit = Physics2D.Linecast(transform.position, groundCheck.position, 1 << LayerMask.NameToLayer("Ground"));
            if(hit.transform != null)
            {
                platforms.Add(hit.transform);
                if(currentPlatform == hit.transform)
                {
                    onSamePlatform = true;
                }
            }
        }

        if(!onSamePlatform)
        {
            foreach(Transform platform in platforms)
            {
                currentPlatform = platform;
                lastPlatformPosition = currentPlatform.position;
            }
        }
    }

    void LateUpdate()
    {
        if(currentPlatform != null)
        {
            //Determine how far platform has moved
            currentPlatformDelta = currentPlatform.position - lastPlatformPosition;

            lastPlatformPosition = currentPlatform.position;
        }
        if(currentPlatform != null)
        {
            //Move with the platform
            transform.position += currentPlatformDelta;
        }
    }   
}

A problem arises when I try to modify the script with a touchable controller. I have googled many times and modified the script as I could, and still it gives me no result (btw, I'm new to Unity). Then I found a tutorial from a website about making a touch controller with a GUI Texture (TouchControls). I think that tutorial is easy to learn. Here is the script

using UnityEngine;
using System.Collections;

[RequireComponent(typeof (Rigidbody2D))]
[RequireComponent(typeof(BoxCollider2D))]
public class TouchControls : MonoBehaviour {

    // GUI textures
    public GUITexture guiLeft;
    public GUITexture guiRight;
    public GUITexture guiJump;
    private Animator anim;
    // Movement variables
    public float moveSpeed = 5f;
    public float jumpForce = 50f;
    public float maxJumpVelocity = 2f;

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

    void Start ()
    {
        anim = gameObject.GetComponent<Animator>();
    }
    // 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;
            }
        }

        // 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;
        }
    }

    void FixedUpdate()
    {
        //anim.SetFloat("Speed", Mathf.Abs);
        // 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;
            }
        }
    }
}

But I have no idea how to implement the script from the tutorial (TouchControls) and assign that to my player control script (PlayerCharacter). How can I combine both scripts so that a player can control it with a touchable control?

Weissberg answered 30/1, 2014 at 10:56 Comment(2)
How exactly are you trying to implement touch controls into your game? Will left/right/jump correspond to certain swipes? Or will they trigger when you touches a certain point on the screen?Roryros
Left, right and jump. Umm for some purpose I Prefer using GUItexture for touch controller, like i post above. But i don't know how to implement itWeissberg
T
1

The best thing you can do is not to drag the touch controls from the touchcontrols tutorial to the playercontroller but the other way around, use the touchcontrols tutorial script as your template.

Since your playercontroller uses floats in its input such as moveleft = 50.0f; and the touchcontrols uses moveleft = true;

the scripts are very different from each other to just merge and work.

so from that in the touchcontrols leave the update function as it is, and only update the fixedupate function with your controls logic since the update void, is the condition controller for right, left, up & down so to speak. and it will also handle the actual input of the touch.

the fixed update could then control some things that the playercontroller has such as apply force when touching a tagged object or stuff like that. and the update only does the input condition, good advice would be to wrap the update touch code in its own function so the update is not only touch but also other game logic related code.

Terle answered 30/1, 2014 at 12:54 Comment(2)
woah, seems to look tough for me. if it does not fit, is there any other method to make touchcontroll suitable for PlayerCharacter code?Weissberg
You could look at the standard assets for mobile that comes with unity, they have some standard controls for 3'rd person and first person, i would set up my character with that, and then open the source and modify it slowly over time to fit my need as the game progrsses in developmentUndine
T
0

You should search use copy the touch control script inside the player controller while changing the right parts. For example, instead of using Input.GetKeyDown you should use the Input.GetTouch but it depends on the game you are creating. You should pay attention to that code and change some parts

Thimbleful answered 1/12, 2020 at 18:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.