[Unity] How to create effect “coin fly” in less than 15 minutes

Do you want make beautiful coin fly effect as game Perfect Park – Brain Challenge?

One of basic feature of any game is “collecting coin bonus” to exchange props.  Make game more attractive and full content by adding collision event between player and coin object. A long time, famous games as Candy Crush using coin fly effect. In this articles, I will share step by step to make coin fly effects in Unity.

Prepare:

  • Coin object prefab in .fbx format. Link at here.
  • Coin image in .png or jpg format. Link at here.
  • Using DOTween for moving coin. You can download free at here.

Implementation:

Step 1: Add coin object to your scene

  • Set tag to “Coin”
  • Set Collider to “Mesh Collider” or Box Collider if you want optimize performance

Coin Object TagBox Collider

Step 2: Add coin image to your canvas UI

  • Add image to canvas. Set tag for this image is CoinTargetPos (will be used for flying coin from world scene to canvas)
  • Add coin text to canvas

 

Step 3: Add code to collision event between player and coin object

Add the following code in to Player scripts:

private void OnCollisionEnter(Collision collision)
{
     if (collision.gameObject.CompareTag(“Coin”))
    {
           //play coin sound (please using your code). This is example.

//Add CoinManager to coin object. This is important for moving coin
CoinManager coinManager = collision.gameObject.AddComponent<CoinManager>();
coinManager.Despawn(1f);

//Increase coin and update coin text (please using your code).

     }    

}

Step 4: CoinManager Scripts to moving coin from WorldScene to Canvas

 Important!
– To move object from World Scene to Canvas, you need convert position of canvas object to World Scene.
– Using DOTween for moving coin. You can download free at here.


using DG.Tweening;
using System.Collections;
using UnityEngine;

public class CoinManager : MonoBehaviour
{
public Transform coinPosTrans;
private Transform coinObjectTrans;

private Vector3 firstPosition;
private Vector3 firstScale;
private Quaternion firstRotation;

private Camera mainCamera;
private Vector3 worldPos;
private bool isDespawned = false;

// Start is called before the first frame update
private void Start()
{
mainCamera = Camera.main;

if (coinPosTrans == null)
{
coinPosTrans = GameObject.FindGameObjectWithTag(“CoinTargetPos”).transform;
}

//Convert position of rectangle transform to world point on Scene
Vector3 screenPoint = coinPosTrans.position + new Vector3(0, 0, 5);
worldPos = mainCamera.ScreenToWorldPoint(screenPoint);

firstPosition = this.transform.position;
firstRotation = this.transform.rotation;
firstScale = this.transform.localScale;

}

// Update is called once per frame
private void FixedUpdate()
{
if (!isDespawned)
{
MoveToCoinPos();
}
}


/// <summary>
/// Move coin to text position on canvas UI
/// </summary>
public void MoveToCoinPos()
{
//move towards the world space positio
transform.localScale = this.transform.localScale / 1.05f;
transform.position = Vector3.MoveTowards(transform.position, worldPos, 10 * Time.deltaTime);
}

/// <summary>
/// Move coin to text position on canvas UI using Tween
/// </summary>
/// <param name=”duration”></param>
public void MoveToCoinPos(float duration)
{
Vector3 path1 = new Vector3(transform.position.x + 0.25f, transform.position.y + 0.25f, transform.position.z);
Vector3 path2 = worldPos;

Vector3[] arrPath = new Vector3[2];
arrPath[0] = path1;
arrPath[1] = path2;

transform.DOLocalPath(arrPath, duration);
}

/// <summary>
/// Disable coin object
/// </summary>
/// <param name=”duration”></param>
public void Despawn(float duration)
{
StartCoroutine(ReturnCoinPos(duration));
}

/// <summary>
/// Return coin object to first position
/// </summary>
private void ReturnFirstPos()
{
//coinObjectTrans.position = firstPosition;
//coinObjectTrans.rotation = firstRotation;
isDespawned = true;
this.transform.position = firstPosition;
this.transform.rotation = firstRotation;
this.transform.localScale = firstScale;
}

/// <summary>
/// Coroutine for return coin first position
/// </summary>
/// <param name=”duration”></param>
/// <returns></returns>
private IEnumerator ReturnCoinPos(float duration)
{
yield return new WaitForSeconds(duration);

//coinObjectTrans.gameObject.SetActive(false);
this.gameObject.SetActive(false);
//isDespawned = true;

ReturnFirstPos();
}

}

If you want try perfect car parking using coin flying effect, please explore at here:

Resources for this tutorial at here.

Cheer!