コインをゲットしよう


コインをゲットする
using UnityEngine;
public class Ball : MonoBehaviour
{
private InputSystem_Actions isa;
public float moveSpeed;
private Rigidbody rb;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
rb = GetComponent<Rigidbody>();
}
void Update()
{
Vector2 movement2 = isa.Player.Move.ReadValue<Vector2>();
Vector3 movement3 = new Vector3(movement2.x, 0, movement2.y);
rb.AddForce(movement3 * moveSpeed);
}
// ★追加
private void OnTriggerEnter(Collider other)
{
// もしもぶつかった相手に「Coin」という「Tag」が付いていたならば(条件)
if (other.CompareTag("Coin"))
{
// ぶつかった相手を破壊する(実行)
Destroy(other.gameObject);
}
}
}









効果音を追加する
using UnityEngine;
public class Ball : MonoBehaviour
{
private InputSystem_Actions isa;
public float moveSpeed;
private Rigidbody rb;
// ★★追加(効果音)
public AudioClip coinSound;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
rb = GetComponent<Rigidbody>();
}
void Update()
{
Vector2 movement2 = isa.Player.Move.ReadValue<Vector2>();
Vector3 movement3 = new Vector3(movement2.x, 0, movement2.y);
rb.AddForce(movement3 * moveSpeed);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
// ★★追加(効果音)
AudioSource.PlayClipAtPoint(coinSound, transform.position);
}
}
}


【Unity6版】BallGame(全27回)
他のコースを見る

コインをゲットする
using UnityEngine;
public class Ball : MonoBehaviour
{
private InputSystem_Actions isa;
public float moveSpeed;
private Rigidbody rb;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
rb = GetComponent<Rigidbody>();
}
void Update()
{
Vector2 movement2 = isa.Player.Move.ReadValue<Vector2>();
Vector3 movement3 = new Vector3(movement2.x, 0, movement2.y);
rb.AddForce(movement3 * moveSpeed);
}
// ★追加
private void OnTriggerEnter(Collider other)
{
// もしもぶつかった相手に「Coin」という「Tag」が付いていたならば(条件)
if (other.CompareTag("Coin"))
{
// ぶつかった相手を破壊する(実行)
Destroy(other.gameObject);
}
}
}









効果音を追加する
using UnityEngine;
public class Ball : MonoBehaviour
{
private InputSystem_Actions isa;
public float moveSpeed;
private Rigidbody rb;
// ★★追加(効果音)
public AudioClip coinSound;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
rb = GetComponent<Rigidbody>();
}
void Update()
{
Vector2 movement2 = isa.Player.Move.ReadValue<Vector2>();
Vector3 movement3 = new Vector3(movement2.x, 0, movement2.y);
rb.AddForce(movement3 * moveSpeed);
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
// ★★追加(効果音)
AudioSource.PlayClipAtPoint(coinSound, transform.position);
}
}
}


コインをゲットしよう