Mobile Game Tutorial part 04 – rocket controller.

Hi, part 4 of mobile game tutorial is available:

CODE:

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

public class RocketController : MonoBehaviour
{
    public Image fuelBarMask;
    public float maxFuel = 10;
    public float fuelConsumptionSpeed = 0.1f;
    public float thrustForce = 10;
    public float strafeSpeed = 5;
    float currentFuel;
    Vector2 touchPos;
    Rigidbody2D rb2d;
    // Start is called before the first frame update
    void Start()
    {
        currentFuel = maxFuel;
        rb2d = GetComponent<Rigidbody2D>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        if (Input.touchCount > 0)
        {
            Touch touch = Input.GetTouch(0);

            switch (touch.phase)
            {
                case TouchPhase.Began:
                    touchPos = Camera.main.ScreenToWorldPoint(touch.position);
                    break;
                case TouchPhase.Moved:
                    touchPos = Camera.main.ScreenToWorldPoint(touch.position);
                    Strafe();
                    AddThrustForce();
                    break;
                case TouchPhase.Stationary:
                    AddThrustForce();
                    Strafe();
                    break;
                case TouchPhase.Ended:
                    break;

            }
        }
    }

    public void AddThrustForce()
    {
        if (currentFuel > 0)
        {
            Vector2 _thrustForceVector = new Vector2(0, thrustForce);
            rb2d.AddForce(_thrustForceVector);
            ConsumeFuel();
        }
    }

    public void ConsumeFuel()
    {
        currentFuel -= fuelConsumptionSpeed; // currentFuel = currentFuel - fuelConsumptionSpeed;

        float fill = currentFuel / maxFuel;
        fuelBarMask.fillAmount = fill;
    }

    public void Strafe()
    {
        Vector2 _strafeVector = new Vector2(touchPos.x, transform.position.y);
        if (currentFuel > 0)
        {
            transform.position = Vector2.MoveTowards(transform.position, _strafeVector, strafeSpeed * Time.fixedDeltaTime);
        }
        if (currentFuel < 0)
        {
            transform.position = Vector2.MoveTowards(transform.position, _strafeVector, (strafeSpeed/3) * Time.fixedDeltaTime);
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{
    public Transform playerPos;
    public float cameraOffsetY;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        FollowPlayer();
    }

    public void FollowPlayer()
    {
        Vector2 _desiredCameraaPos = new Vector2(0, playerPos.position.y + cameraOffsetY);
        transform.position = _desiredCameraaPos;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *