(サンプルコード)
using UnityEngine;
public class Ball : MonoBehaviour
{
private InputSystem_Actions isa;
public float moveSpeed;
private Rigidbody rb;
public float jumpPower;
private bool isJumping = false;
// ★コインの獲得
private int coinCount = 0;
public GameObject[] coinIcons;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
rb = GetComponent<Rigidbody>();
}
void Update()
{
Vector2 movement2 = isa.Player.Move.ReadValue();
Vector3 movement3 = new Vector3(movement2.x, 0, movement2.y);
Vector3 desiredMove = Camera.main.transform.forward * movement3.z + Camera.main.transform.right * movement3.x;
rb.AddForce(desiredMove * moveSpeed);
if (isa.Player.Jump.triggered && isJumping == false)
{
rb.linearVelocity += Vector3.up * jumpPower;
isJumping = true;
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
// ★コインの獲得
private void OnTriggerEnter(Collider other)
{
if(other.CompareTag("Coin"))
{
Destroy(other.gameObject);
coinCount += 1;
// (ポイント)
// コインアイコンを1つずつ消していく(オフにする)
coinIcons[coinCount - 1].SetActive(false);
}
}
}
(設定)
(実行確認)
・コインを獲得するたびごとに、アイコンの数が減れば成功です。