ボールのジャンプ方法を変える
タップでボールをジャンプさせる
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 (Application.isEditor)
{
if (Input.GetButtonDown("Jump") && isJumping == false)
{
rb.velocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
else
{
// Fire1はデフォルトでは「画面タップ」と結びついています。
if (Input.GetButtonDown("Fire1") && 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;
}
}
}
タップでボールをジャンプさせる
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 (Application.isEditor)
{
if (Input.GetButtonDown("Jump") && isJumping == false)
{
rb.velocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
else
{
// Fire1はデフォルトでは「画面タップ」と結びついています。
if (Input.GetButtonDown("Fire1") && 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;
}
}
}
ボールのジャンプ方法を変える