So i added something as cool as koolade


So i wanted to be able to play my trashy game on my phone, so i added mobile controls,

it detects whether or not you are playing on  a computer by sensing your touch, then changes your controls accordingly,

its by no means perfect, i don't have the stopping down, but it it helps i will paste my script.

i watched a few tutorials, but for the most part it is my script:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Security.Cryptography;
using UnityEngine;

public class Movement : MonoBehaviour
{
    public bool FacingRight;
    public bool isPhone;
    public bool isStopped;

    private float screenWidth;
    public float speed = 5f;
    public float phoneSpeed;
    float moveAM;

    public Animator am;
    public Rigidbody2D rb;

    void Start()
    {
        screenWidth = Screen.width / 2;
    }
    void Update()
    {
        if (isPhone)
        {
            if(Input.touchCount > 0)
            {
                Touch touch = Input.GetTouch(0);
                switch (touch.phase)
                {
                    case TouchPhase.Began:

                        isStopped = true;

                        break;

                    case TouchPhase.Ended:

                        isStopped = false;
                        Turn();

                        break;
                }
            }
        }
    }
    void FixedUpdate()
    {
        keyboard();
        touchControl();
        
    }
    public void keyboard()
    {
        moveAM = Input.GetAxisRaw("Horizontal");
        am.SetFloat("Speed", Mathf.Abs(moveAM));

        Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
        transform.position += move * Time.deltaTime * speed;

        if (FacingRight == false && moveAM > 0)
        {
            Turn();
        }
        else if (FacingRight == true && moveAM < 0)
        {
            Turn();
        }
    }
    public void touchControl()
    {
        if (Input.touchCount > 0)
        {
            //Turn();
            isPhone = true;
        }
        if (isPhone && !isStopped)
        {
            if (FacingRight == true)
            {
                transform.Translate(phoneSpeed, 0, 0);
            }
            if (FacingRight == false)
            {
                transform.Translate(-phoneSpeed, 0, 0);
            }
        }
    }
    void Turn()
    {
        FacingRight = !FacingRight;
        Vector3 Scaler = transform.localScale;
        Scaler.x *= -1;
        transform.localScale = Scaler;
    }
}

Get NomNom-Toast Duck

Comments

Log in with itch.io to leave a comment.

This script became outdated when i updated the touch controls