コインをゲットしよう
![B3e27ffa 10e4 4d42 8bc3 aae9b1c97320](https://codegenius.org/uploads/slide/image/2869/b3e27ffa-10e4-4d42-8bc3-aae9b1c97320.jpeg)
![F8e1dc67 c8ce 45bd 96cb 3fe1f68f8962](https://codegenius.org/uploads/slide/image/2870/f8e1dc67-c8ce-45bd-96cb-3fe1f68f8962.jpeg)
コインゲット
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveH, 0, moveV);
rb.AddForce(movement * moveSpeed);
}
private void OnTriggerEnter(Collider other)
{
// もしもぶつかった相手に「Coin」という「Tag」が付いていたならば(条件)
if (other.CompareTag("Coin"))
{
// ぶつかった相手を破壊する(実行)
Destroy(other.gameObject);
}
}
}
![23679308 b3c0 4d03 99eb 069b0c089fa8](https://codegenius.org/uploads/slide/image/2871/23679308-b3c0-4d03-99eb-069b0c089fa8.jpeg)
![11787b30 8c06 41c3 8c0a 0a0e8c672fe9](https://codegenius.org/uploads/slide/image/2872/11787b30-8c06-41c3-8c0a-0a0e8c672fe9.jpeg)
![601536a2 af48 419c bcfa 347328cc43c8](https://codegenius.org/uploads/slide/image/2873/601536a2-af48-419c-bcfa-347328cc43c8.jpeg)
![Ed64c7f4 a2c8 4104 b6e9 cae2e3058fca](https://codegenius.org/uploads/slide/image/2874/ed64c7f4-a2c8-4104-b6e9-cae2e3058fca.jpeg)
![5cc1e1ec 4cb7 4f3b a8bd 3425e574c9f0](https://codegenius.org/uploads/slide/image/2875/5cc1e1ec-4cb7-4f3b-a8bd-3425e574c9f0.jpeg)
![71b25218 9db6 45fa 97a9 71c76e2b414e](https://codegenius.org/uploads/slide/image/2876/71b25218-9db6-45fa-97a9-71c76e2b414e.jpeg)
![Cc065070 a7c8 4e62 8034 c4c04a6318e1](https://codegenius.org/uploads/slide/image/2877/cc065070-a7c8-4e62-8034-c4c04a6318e1.jpeg)
![C4fe3445 86c5 4e1d 83be dbdd4b60a1f9](https://codegenius.org/uploads/slide/image/2878/c4fe3445-86c5-4e1d-83be-dbdd4b60a1f9.jpeg)
![Ccb8b90f b75b 4a10 9cdb 49ad461c1276](https://codegenius.org/uploads/slide/image/2879/ccb8b90f-b75b-4a10-9cdb-49ad461c1276.jpeg)
![19c87a64 5dd5 4d07 9057 3c30a833c770](https://codegenius.org/uploads/slide/image/2880/19c87a64-5dd5-4d07-9057-3c30a833c770.jpeg)
効果音の追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
// ★★追加(効果音)
public AudioClip coinGet;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveH, 0, moveV);
rb.AddForce(movement * moveSpeed);
}
// ★追加
private void OnTriggerEnter(Collider other)
{
// もしもぶつかった相手に「Coin」という「Tag」が付いていたならば(条件)
if (other.CompareTag("Coin"))
{
// ぶつかった相手を破壊する(実行)
Destroy(other.gameObject);
// ★★追加(効果音)
AudioSource.PlayClipAtPoint(coinGet, transform.position);
}
}
}
![E84af2ed c14f 41e3 9e8a 8ddb70ba90aa](https://codegenius.org/uploads/slide/image/2881/e84af2ed-c14f-41e3-9e8a-8ddb70ba90aa.jpeg)
![0495c6da 4fed 4984 b1ae 3a5115c9263e](https://codegenius.org/uploads/slide/image/2882/0495c6da-4fed-4984-b1ae-3a5115c9263e.jpeg)
【2018版】BallGame(全25回)
他のコースを見る![B3e27ffa 10e4 4d42 8bc3 aae9b1c97320](https://codegenius.org/uploads/slide/image/2869/b3e27ffa-10e4-4d42-8bc3-aae9b1c97320.jpeg)
![F8e1dc67 c8ce 45bd 96cb 3fe1f68f8962](https://codegenius.org/uploads/slide/image/2870/f8e1dc67-c8ce-45bd-96cb-3fe1f68f8962.jpeg)
コインゲット
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveH, 0, moveV);
rb.AddForce(movement * moveSpeed);
}
private void OnTriggerEnter(Collider other)
{
// もしもぶつかった相手に「Coin」という「Tag」が付いていたならば(条件)
if (other.CompareTag("Coin"))
{
// ぶつかった相手を破壊する(実行)
Destroy(other.gameObject);
}
}
}
![23679308 b3c0 4d03 99eb 069b0c089fa8](https://codegenius.org/uploads/slide/image/2871/23679308-b3c0-4d03-99eb-069b0c089fa8.jpeg)
![11787b30 8c06 41c3 8c0a 0a0e8c672fe9](https://codegenius.org/uploads/slide/image/2872/11787b30-8c06-41c3-8c0a-0a0e8c672fe9.jpeg)
![601536a2 af48 419c bcfa 347328cc43c8](https://codegenius.org/uploads/slide/image/2873/601536a2-af48-419c-bcfa-347328cc43c8.jpeg)
![Ed64c7f4 a2c8 4104 b6e9 cae2e3058fca](https://codegenius.org/uploads/slide/image/2874/ed64c7f4-a2c8-4104-b6e9-cae2e3058fca.jpeg)
![5cc1e1ec 4cb7 4f3b a8bd 3425e574c9f0](https://codegenius.org/uploads/slide/image/2875/5cc1e1ec-4cb7-4f3b-a8bd-3425e574c9f0.jpeg)
![71b25218 9db6 45fa 97a9 71c76e2b414e](https://codegenius.org/uploads/slide/image/2876/71b25218-9db6-45fa-97a9-71c76e2b414e.jpeg)
![Cc065070 a7c8 4e62 8034 c4c04a6318e1](https://codegenius.org/uploads/slide/image/2877/cc065070-a7c8-4e62-8034-c4c04a6318e1.jpeg)
![C4fe3445 86c5 4e1d 83be dbdd4b60a1f9](https://codegenius.org/uploads/slide/image/2878/c4fe3445-86c5-4e1d-83be-dbdd4b60a1f9.jpeg)
![Ccb8b90f b75b 4a10 9cdb 49ad461c1276](https://codegenius.org/uploads/slide/image/2879/ccb8b90f-b75b-4a10-9cdb-49ad461c1276.jpeg)
![19c87a64 5dd5 4d07 9057 3c30a833c770](https://codegenius.org/uploads/slide/image/2880/19c87a64-5dd5-4d07-9057-3c30a833c770.jpeg)
効果音の追加
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
// ★★追加(効果音)
public AudioClip coinGet;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveH, 0, moveV);
rb.AddForce(movement * moveSpeed);
}
// ★追加
private void OnTriggerEnter(Collider other)
{
// もしもぶつかった相手に「Coin」という「Tag」が付いていたならば(条件)
if (other.CompareTag("Coin"))
{
// ぶつかった相手を破壊する(実行)
Destroy(other.gameObject);
// ★★追加(効果音)
AudioSource.PlayClipAtPoint(coinGet, transform.position);
}
}
}
![E84af2ed c14f 41e3 9e8a 8ddb70ba90aa](https://codegenius.org/uploads/slide/image/2881/e84af2ed-c14f-41e3-9e8a-8ddb70ba90aa.jpeg)
![0495c6da 4fed 4984 b1ae 3a5115c9263e](https://codegenius.org/uploads/slide/image/2882/0495c6da-4fed-4984-b1ae-3a5115c9263e.jpeg)
コインをゲットしよう