Character movement via on-screen touch controller input | Rigidbody | Unity Game Engine


TouchController.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class TouchController : MonoBehaviour
{
    public static float Horizontal = 0, Vertical = 0;

    void Awake()
    {
        SetupButton(transform.Find("up").gameObject, Vector2.up);
        SetupButton(transform.Find("down").gameObject, Vector2.down);
        SetupButton(transform.Find("left").gameObject, Vector2.left);
        SetupButton(transform.Find("right").gameObject, Vector2.right);
    }

    void SetupButton(GameObject buttonObject, Vector2 position)
    {
        EventTrigger eventTrigger = buttonObject.AddComponent<EventTrigger>();

        var pointerDown = new EventTrigger.Entry();
        pointerDown.eventID = EventTriggerType.PointerDown;
        pointerDown.callback.AddListener((e) => UpdateAxisValue(position));
        eventTrigger.triggers.Add(pointerDown);

        var pointerUp = new EventTrigger.Entry();
        pointerUp.eventID = EventTriggerType.PointerUp;
        pointerUp.callback.AddListener((e) => UpdateAxisValue(position, reset: true));
        eventTrigger.triggers.Add(pointerUp);
    }

    void UpdateAxisValue(Vector2 position, bool reset = false)
    {
        if (position.x == 0)
            Vertical = reset ? 0 : position.y;
        else
            Horizontal = reset ? 0 : position.x;
    }
}

TouchMove.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class TouchMove : MonoBehaviour
{
    public float speed = 5;
    Rigidbody rigidBody;

    void Awake()
    {
        rigidBody = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        float inputH = TouchController.Horizontal;
        float inputV = TouchController.Vertical;
        rigidBody.velocity = new Vector3(inputH * speed, rigidBody.velocity.y, inputV * speed);
    }
}

Character movement via keyboard/controller input | Rigidbody | Unity Game Engine


Move.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class Move : MonoBehaviour
{
    public float speed = 5;
    Rigidbody rigidBody;

    void Awake()
    {
        rigidBody = GetComponent<Rigidbody>();
    }

    void FixedUpdate()
    {
        float inputH = Input.GetAxis("Horizontal");
        float inputV = Input.GetAxis("Vertical");
        rigidBody.velocity = new Vector3(inputH * speed, rigidBody.velocity.y, inputV * speed);
    }
}

Double Jump | Rigidbody | Unity Game Engine


DoubleJump.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class DoubleJump : MonoBehaviour
{
    public float jumpForce = 5;
    public float groundDistance = 0.5f;

    Rigidbody rigidBody;
    bool canDoubleJump;

    void Awake()
    {
        rigidBody = GetComponent<Rigidbody>();
    }

    bool IsGrounded()
    {
        return Physics.Raycast(transform.position, Vector3.down, groundDistance);
    }

    void Update()
    {
        if(Input.GetMouseButtonDown(0))
        {
            if(IsGrounded())
            {
                rigidBody.velocity = Vector3.up * jumpForce;
                canDoubleJump = true;
            }
            else if(canDoubleJump)
            {
                rigidBody.velocity = Vector3.up * jumpForce;
                canDoubleJump = false;
            }
        }
    }
}

Single Jump | Rigidbody | Unity Game Engine


SingleJump.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class SingleJump : MonoBehaviour
{
    public float jumpForce = 5;
    public float groundDistance = 0.5f;

    Rigidbody rigidBody;

    void Awake()
    {
        rigidBody = GetComponent<Rigidbody>();
    }

    bool IsGrounded()
    {
        return Physics.Raycast(transform.position, Vector3.down, groundDistance);
    }

    void Update()
    {
        if(Input.GetMouseButtonDown(0) && IsGrounded())
        {
            rigidBody.velocity = Vector3.up * jumpForce;
        }
    }
}

Continuous Jump | Rigidbody | Unity Game Engine


Jump.cs
using UnityEngine;

[RequireComponent(typeof(Rigidbody))]
public class Jump : MonoBehaviour
{
    public float jumpForce = 4;
    public float jumpInterval = 0.25f;

    Rigidbody rigidBody;
    float jumpTimer;

    void Awake()
    {
        rigidBody = GetComponent<Rigidbody>();
    }

    void Update()
    {
        jumpTimer += Time.deltaTime;
        if (Input.GetMouseButtonDown(0))
        {
            if (jumpTimer > jumpInterval)
            {
                jumpTimer = 0;
                rigidBody.velocity = Vector3.up * jumpForce;
            }
        }
    }
}

Display a number counting towards the target number | Unity Game Engine


Count2Target.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Text))]
public class Count2Target : MonoBehaviour
{
    public float countDuration = 1;
    Text numberText;
    float currentValue = 0, targetValue = 0;
    Coroutine _C2T;

    void Awake()
    {
        numberText = GetComponent<Text>();
    }

    void Start()
    {
        currentValue = float.Parse(numberText.text);
        targetValue = currentValue;
    }

    IEnumerator CountTo(float targetValue)
    {
        var rate = Mathf.Abs(targetValue - currentValue) / countDuration;
        while(currentValue != targetValue)
        {
            currentValue = Mathf.MoveTowards(currentValue, targetValue, rate * Time.deltaTime);
            numberText.text = ((int)currentValue).ToString();
            yield return null;
        }
    }

    public void AddValue(float value)
    {
        targetValue += value;
        if (_C2T != null)
            StopCoroutine(_C2T);
        _C2T = StartCoroutine(CountTo(targetValue));
    }

    public void SetTarget(float target)
    {
        targetValue = target;
        if (_C2T != null)
            StopCoroutine(_C2T);
        _C2T = StartCoroutine(CountTo(targetValue));
    }
}