コインをゲットしよう
![51ee9f72 3601 491c 9280 9fda302fb757](https://codegenius.org/uploads/slide/image/3540/51ee9f72-3601-491c-9280-9fda302fb757.jpeg)
![97366561 5327 4b98 bd74 4c351783ab39](https://codegenius.org/uploads/slide/image/3541/97366561-5327-4b98-bd74-4c351783ab39.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);
}
}
}
![07c36960 0abc 4a63 a910 076367480743](https://codegenius.org/uploads/slide/image/3542/07c36960-0abc-4a63-a910-076367480743.jpeg)
![Ca46e809 ddb6 463f 82ff cc73c7d4cfae](https://codegenius.org/uploads/slide/image/3543/ca46e809-ddb6-463f-82ff-cc73c7d4cfae.jpeg)
![16b012db 6987 4719 9d7b 433c28834000](https://codegenius.org/uploads/slide/image/3544/16b012db-6987-4719-9d7b-433c28834000.jpeg)
![D506b34c 1ccf 446b ba1c cff7c45101e5](https://codegenius.org/uploads/slide/image/3545/d506b34c-1ccf-446b-ba1c-cff7c45101e5.jpeg)
![4857b2fe 49cf 433d b3c6 79565d01dbd3](https://codegenius.org/uploads/slide/image/3546/4857b2fe-49cf-433d-b3c6-79565d01dbd3.jpeg)
![814b4ac4 8778 468f a1c7 12ebe3dca5ea](https://codegenius.org/uploads/slide/image/3547/814b4ac4-8778-468f-a1c7-12ebe3dca5ea.jpeg)
![2f8584fd f90a 4b27 bdc4 d6310b9d5a09](https://codegenius.org/uploads/slide/image/3548/2f8584fd-f90a-4b27-bdc4-d6310b9d5a09.jpeg)
![C3921924 958f 4f9d 8376 9bea5474ce54](https://codegenius.org/uploads/slide/image/3549/c3921924-958f-4f9d-8376-9bea5474ce54.jpeg)
![06edb287 b41c 428a a4aa 963a4a8fe8f5](https://codegenius.org/uploads/slide/image/3550/06edb287-b41c-428a-a4aa-963a4a8fe8f5.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)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
// ★★追加(効果音)
AudioSource.PlayClipAtPoint(coinGet, transform.position);
}
}
}
![Ebb55ee5 4a4b 4b2a 8121 259f74154d48](https://codegenius.org/uploads/slide/image/3551/ebb55ee5-4a4b-4b2a-8121-259f74154d48.jpeg)
![7db1d2b1 936a 4ab3 ba8e c9747fb0b91d](https://codegenius.org/uploads/slide/image/3552/7db1d2b1-936a-4ab3-ba8e-c9747fb0b91d.jpeg)
【2019版】BallGame(全27回)
他のコースを見る![51ee9f72 3601 491c 9280 9fda302fb757](https://codegenius.org/uploads/slide/image/3540/51ee9f72-3601-491c-9280-9fda302fb757.jpeg)
![97366561 5327 4b98 bd74 4c351783ab39](https://codegenius.org/uploads/slide/image/3541/97366561-5327-4b98-bd74-4c351783ab39.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);
}
}
}
![07c36960 0abc 4a63 a910 076367480743](https://codegenius.org/uploads/slide/image/3542/07c36960-0abc-4a63-a910-076367480743.jpeg)
![Ca46e809 ddb6 463f 82ff cc73c7d4cfae](https://codegenius.org/uploads/slide/image/3543/ca46e809-ddb6-463f-82ff-cc73c7d4cfae.jpeg)
![16b012db 6987 4719 9d7b 433c28834000](https://codegenius.org/uploads/slide/image/3544/16b012db-6987-4719-9d7b-433c28834000.jpeg)
![D506b34c 1ccf 446b ba1c cff7c45101e5](https://codegenius.org/uploads/slide/image/3545/d506b34c-1ccf-446b-ba1c-cff7c45101e5.jpeg)
![4857b2fe 49cf 433d b3c6 79565d01dbd3](https://codegenius.org/uploads/slide/image/3546/4857b2fe-49cf-433d-b3c6-79565d01dbd3.jpeg)
![814b4ac4 8778 468f a1c7 12ebe3dca5ea](https://codegenius.org/uploads/slide/image/3547/814b4ac4-8778-468f-a1c7-12ebe3dca5ea.jpeg)
![2f8584fd f90a 4b27 bdc4 d6310b9d5a09](https://codegenius.org/uploads/slide/image/3548/2f8584fd-f90a-4b27-bdc4-d6310b9d5a09.jpeg)
![C3921924 958f 4f9d 8376 9bea5474ce54](https://codegenius.org/uploads/slide/image/3549/c3921924-958f-4f9d-8376-9bea5474ce54.jpeg)
![06edb287 b41c 428a a4aa 963a4a8fe8f5](https://codegenius.org/uploads/slide/image/3550/06edb287-b41c-428a-a4aa-963a4a8fe8f5.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)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
// ★★追加(効果音)
AudioSource.PlayClipAtPoint(coinGet, transform.position);
}
}
}
![Ebb55ee5 4a4b 4b2a 8121 259f74154d48](https://codegenius.org/uploads/slide/image/3551/ebb55ee5-4a4b-4b2a-8121-259f74154d48.jpeg)
![7db1d2b1 936a 4ab3 ba8e c9747fb0b91d](https://codegenius.org/uploads/slide/image/3552/7db1d2b1-936a-4ab3-ba8e-c9747fb0b91d.jpeg)
コインをゲットしよう