ボールを動かす方法を変える
![2396f307 60e6 4e5d 9c71 01fd41219c15](https://codegenius.org/uploads/slide/image/3033/2396f307-60e6-4e5d-9c71-01fd41219c15.jpeg)
![2a322558 7efb 4e58 adeb 465b6aa1aeec](https://codegenius.org/uploads/slide/image/3034/2a322558-7efb-4e58-adeb-465b6aa1aeec.jpeg)
![53577856 44c4 43a5 9bed c49410830d6f](https://codegenius.org/uploads/slide/image/3035/53577856-44c4-43a5-9bed-c49410830d6f.jpeg)
加速度センサーでボールを動かす
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinGet;
public float jumpSpeed;
private bool isJumping = false;
private int coinCount = 0;
public GameObject target;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
// ★ボールの操作方法の修正
if (Application.isEditor)
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveH, 0, moveV);
rb.AddForce(movement * moveSpeed);
}
else
{
// スマホやタブレットの加速度センサーを活用する。
float moveH = Input.acceleration.x;
float moveV = Input.acceleration.y; // ここがポイント!
Vector3 movement = new Vector3(moveH, 0, moveV);
rb.AddForce(movement * moveSpeed);
}
if (Input.GetButtonDown("Jump") && isJumping == false)
{
rb.velocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinGet, transform.position);
coinCount += 1;
if (coinCount == 1)
{
target.SetActive(true);
}
if (coinCount == 2)
{
SceneManager.LoadScene("GameClear");
}
}
}
public int Coin()
{
return coinCount;
}
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![44410ced 45e3 494c 94d5 1d8178bb040f](https://codegenius.org/uploads/slide/image/3036/44410ced-45e3-494c-94d5-1d8178bb040f.jpeg)
![3ea60cd2 1464 444f af05 d7ebdb2177cb](https://codegenius.org/uploads/slide/image/3037/3ea60cd2-1464-444f-af05-d7ebdb2177cb.jpeg)
![0f815708 ed14 4bd3 903b 41c2004a7763](https://codegenius.org/uploads/slide/image/3038/0f815708-ed14-4bd3-903b-41c2004a7763.jpeg)
![345523a4 cd0a 4507 b2dc e34d29eb8ba0](https://codegenius.org/uploads/slide/image/3039/345523a4-cd0a-4507-b2dc-e34d29eb8ba0.jpeg)
![1003e4e6 17d5 447d 80e2 19244a63d188](https://codegenius.org/uploads/slide/image/3040/1003e4e6-17d5-447d-80e2-19244a63d188.jpeg)
![6b14dcd0 422a 4e0b a956 7901b6650a04](https://codegenius.org/uploads/slide/image/3041/6b14dcd0-422a-4e0b-a956-7901b6650a04.jpeg)
![Ab2614fe 4d58 42ff 8144 be5ec52ad2ce](https://codegenius.org/uploads/slide/image/3042/ab2614fe-4d58-42ff-8144-be5ec52ad2ce.jpeg)
![C6f53611 780f 4581 9422 e4d503fdcaa8](https://codegenius.org/uploads/slide/image/3043/c6f53611-780f-4581-9422-e4d503fdcaa8.jpeg)
![25ea47ba 0521 4dac 99af 9a182a0cf9a3](https://codegenius.org/uploads/slide/image/3044/25ea47ba-0521-4dac-99af-9a182a0cf9a3.jpeg)
![2396f307 60e6 4e5d 9c71 01fd41219c15](https://codegenius.org/uploads/slide/image/3033/2396f307-60e6-4e5d-9c71-01fd41219c15.jpeg)
![2a322558 7efb 4e58 adeb 465b6aa1aeec](https://codegenius.org/uploads/slide/image/3034/2a322558-7efb-4e58-adeb-465b6aa1aeec.jpeg)
![53577856 44c4 43a5 9bed c49410830d6f](https://codegenius.org/uploads/slide/image/3035/53577856-44c4-43a5-9bed-c49410830d6f.jpeg)
加速度センサーでボールを動かす
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinGet;
public float jumpSpeed;
private bool isJumping = false;
private int coinCount = 0;
public GameObject target;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void Update()
{
// ★ボールの操作方法の修正
if (Application.isEditor)
{
float moveH = Input.GetAxis("Horizontal");
float moveV = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveH, 0, moveV);
rb.AddForce(movement * moveSpeed);
}
else
{
// スマホやタブレットの加速度センサーを活用する。
float moveH = Input.acceleration.x;
float moveV = Input.acceleration.y; // ここがポイント!
Vector3 movement = new Vector3(moveH, 0, moveV);
rb.AddForce(movement * moveSpeed);
}
if (Input.GetButtonDown("Jump") && isJumping == false)
{
rb.velocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinGet, transform.position);
coinCount += 1;
if (coinCount == 1)
{
target.SetActive(true);
}
if (coinCount == 2)
{
SceneManager.LoadScene("GameClear");
}
}
}
public int Coin()
{
return coinCount;
}
private void OnCollisionEnter(Collision other)
{
if (other.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![44410ced 45e3 494c 94d5 1d8178bb040f](https://codegenius.org/uploads/slide/image/3036/44410ced-45e3-494c-94d5-1d8178bb040f.jpeg)
![3ea60cd2 1464 444f af05 d7ebdb2177cb](https://codegenius.org/uploads/slide/image/3037/3ea60cd2-1464-444f-af05-d7ebdb2177cb.jpeg)
![0f815708 ed14 4bd3 903b 41c2004a7763](https://codegenius.org/uploads/slide/image/3038/0f815708-ed14-4bd3-903b-41c2004a7763.jpeg)
![345523a4 cd0a 4507 b2dc e34d29eb8ba0](https://codegenius.org/uploads/slide/image/3039/345523a4-cd0a-4507-b2dc-e34d29eb8ba0.jpeg)
![1003e4e6 17d5 447d 80e2 19244a63d188](https://codegenius.org/uploads/slide/image/3040/1003e4e6-17d5-447d-80e2-19244a63d188.jpeg)
![6b14dcd0 422a 4e0b a956 7901b6650a04](https://codegenius.org/uploads/slide/image/3041/6b14dcd0-422a-4e0b-a956-7901b6650a04.jpeg)
![Ab2614fe 4d58 42ff 8144 be5ec52ad2ce](https://codegenius.org/uploads/slide/image/3042/ab2614fe-4d58-42ff-8144-be5ec52ad2ce.jpeg)
![C6f53611 780f 4581 9422 e4d503fdcaa8](https://codegenius.org/uploads/slide/image/3043/c6f53611-780f-4581-9422-e4d503fdcaa8.jpeg)
![25ea47ba 0521 4dac 99af 9a182a0cf9a3](https://codegenius.org/uploads/slide/image/3044/25ea47ba-0521-4dac-99af-9a182a0cf9a3.jpeg)
ボールを動かす方法を変える