Hi, new episode of mobile game tutorial is available, in this tutorial i will show you how to make pickable objects:
CODE:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BonusMoney : MonoBehaviour
{
public float bonusMoney = 5;
// Start is called before the first frame update
private void OnTriggerEnter2D(Collider2D collision)
{
if(collision.gameObject.tag == "Player")
{
GameManager.instance.AddMoney(bonusMoney);
//play particle system
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BonusFuel : MonoBehaviour
{
public float refillAmount = 0.2f;
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
collision.GetComponent<RocketController>().AddFuel(refillAmount);
//play particle system
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Obstackle : MonoBehaviour
{
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Player")
{
GameManager.instance.PauseGame();
Destroy(collision.gameObject);
//play particle system
Destroy(gameObject);
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
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;
float startPos;
float longestDstTravelled = 0;
public TextMeshProUGUI dstTravelledTXT;
// Start is called before the first frame update
void Start()
{
currentFuel = maxFuel;
rb2d = GetComponent<Rigidbody2D>();
startPos = this.transform.position.y;
longestDstTravelled = 0;
}
// Update is called once per frame
void FixedUpdate()
{
if(!GameManager.instance.GamePaused)
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;
}
}
}
private void Update()
{
if(!GameManager.instance.GamePaused)
DistanceTravelled();
}
public void DistanceTravelled()
{
float currentDstTravelled = this.transform.position.y - startPos;
if (currentDstTravelled < 0)
{
currentDstTravelled = 0;
}
if (currentDstTravelled > longestDstTravelled)
{
longestDstTravelled = currentDstTravelled;
}
dstTravelledTXT.text = currentDstTravelled.ToString("F1") + " m";
if(currentFuel<=0 && rb2d.velocity.y <= 0)
{
GameManager.instance.PauseGame();
GameManager.instance.AddMoney(longestDstTravelled);
}
}
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);
}
}
public void AddFuel(float _fuelRefillAmount)
{
currentFuel += maxFuel * _fuelRefillAmount;
if (currentFuel > maxFuel)
{
currentFuel = maxFuel;
}
}
}