ボールをジャンプさせよう
![D1fc116c 5f52 4852 8584 3935564fbcc0](https://codegenius.org/uploads/slide/image/1874/d1fc116c-5f52-4852-8584-3935564fbcc0.jpeg)
ボールをジャンプさせる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinGet;
// ★追加(ジャンプ)
public float jumpSpeed;
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);
// ★追加(ジャンプ)
if (Input.GetButtonDown("Jump"))
{
rb.velocity = Vector3.up * jumpSpeed;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinGet, transform.position);
}
}
}
![9bddc72f 0aa6 48ac 9662 dc684d6c28bd](https://codegenius.org/uploads/slide/image/1875/9bddc72f-0aa6-48ac-9662-dc684d6c28bd.jpeg)
![9b9478d7 04c6 4c2d 96e6 0c3a379a0969](https://codegenius.org/uploads/slide/image/1876/9b9478d7-04c6-4c2d-96e6-0c3a379a0969.jpeg)
![E5ee23da 03a3 444e a70a 47ecf96185c0](https://codegenius.org/uploads/slide/image/1877/e5ee23da-03a3-444e-a70a-47ecf96185c0.jpeg)
空中(二段)ジャンプの禁止
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinGet;
public float jumpSpeed;
// ★★改良(空中ジャンプ禁止)
// boolは「真偽型」と呼ばれ「true」と「false」という2つの値を扱うことができます。
private bool isJumping = false;
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);
// ★★改良(空中ジャンプ禁止)
// 「&&」は「包含関係の『かつ』の意味」
// (例)「A && B」AかつB・・・>AとBの両方の条件が揃った時
// 「==」は「左右が等しい」という意味
if (Input.GetButtonDown("Jump") && isJumping == false)
{
rb.velocity = Vector3.up * jumpSpeed;
// isJumpingという名前の箱の中身を「true」にする。
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinGet, transform.position);
}
}
}
![4e4c9f70 8fd3 445f ba17 cc16f615e64d](https://codegenius.org/uploads/slide/image/1878/4e4c9f70-8fd3-445f-ba17-cc16f615e64d.jpeg)
![Eaf2db13 e8db 4df7 b2ed 517fa7b4560c](https://codegenius.org/uploads/slide/image/1879/eaf2db13-e8db-4df7-b2ed-517fa7b4560c.jpeg)
ジャンプを復活させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinGet;
public float jumpSpeed;
private bool isJumping = false;
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);
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);
}
}
// ★★★改良(ジャンプの復活)
// OnCollisionEnterのスペルに注意
private void OnCollisionEnter(Collision other)
{
// もしも「Floor」という「Tag」がついたオブジェクトにぶつかったら(条件)
if (other.gameObject.CompareTag("Floor"))
{
// isJumpingの箱の中のデータをfalseにする。
isJumping = false;
}
}
}
![D8ade4de 08b8 4edc 8498 1ab98a0b1c9d](https://codegenius.org/uploads/slide/image/1880/d8ade4de-08b8-4edc-8498-1ab98a0b1c9d.jpeg)
![A94efb35 8e6a 43b1 9bdc c4d5e96e6e26](https://codegenius.org/uploads/slide/image/1881/a94efb35-8e6a-43b1-9bdc-c4d5e96e6e26.jpeg)
![057af899 2103 4a2d 924c 61dc13e73faf](https://codegenius.org/uploads/slide/image/1882/057af899-2103-4a2d-924c-61dc13e73faf.jpeg)
![2d808b8a 6445 4142 86eb 94f43bf32226](https://codegenius.org/uploads/slide/image/1883/2d808b8a-6445-4142-86eb-94f43bf32226.jpeg)
![25b1e330 0f1b 4ce7 b3d6 2cfc28980f83](https://codegenius.org/uploads/slide/image/1884/25b1e330-0f1b-4ce7-b3d6-2cfc28980f83.jpeg)
![140e8633 b180 4cf1 b5ec 22e863789d12](https://codegenius.org/uploads/slide/image/1885/140e8633-b180-4cf1-b5ec-22e863789d12.jpeg)
【2018版】BallGame(全25回)
他のコースを見る![D1fc116c 5f52 4852 8584 3935564fbcc0](https://codegenius.org/uploads/slide/image/1874/d1fc116c-5f52-4852-8584-3935564fbcc0.jpeg)
ボールをジャンプさせる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinGet;
// ★追加(ジャンプ)
public float jumpSpeed;
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);
// ★追加(ジャンプ)
if (Input.GetButtonDown("Jump"))
{
rb.velocity = Vector3.up * jumpSpeed;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinGet, transform.position);
}
}
}
![9bddc72f 0aa6 48ac 9662 dc684d6c28bd](https://codegenius.org/uploads/slide/image/1875/9bddc72f-0aa6-48ac-9662-dc684d6c28bd.jpeg)
![9b9478d7 04c6 4c2d 96e6 0c3a379a0969](https://codegenius.org/uploads/slide/image/1876/9b9478d7-04c6-4c2d-96e6-0c3a379a0969.jpeg)
![E5ee23da 03a3 444e a70a 47ecf96185c0](https://codegenius.org/uploads/slide/image/1877/e5ee23da-03a3-444e-a70a-47ecf96185c0.jpeg)
空中(二段)ジャンプの禁止
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinGet;
public float jumpSpeed;
// ★★改良(空中ジャンプ禁止)
// boolは「真偽型」と呼ばれ「true」と「false」という2つの値を扱うことができます。
private bool isJumping = false;
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);
// ★★改良(空中ジャンプ禁止)
// 「&&」は「包含関係の『かつ』の意味」
// (例)「A && B」AかつB・・・>AとBの両方の条件が揃った時
// 「==」は「左右が等しい」という意味
if (Input.GetButtonDown("Jump") && isJumping == false)
{
rb.velocity = Vector3.up * jumpSpeed;
// isJumpingという名前の箱の中身を「true」にする。
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinGet, transform.position);
}
}
}
![4e4c9f70 8fd3 445f ba17 cc16f615e64d](https://codegenius.org/uploads/slide/image/1878/4e4c9f70-8fd3-445f-ba17-cc16f615e64d.jpeg)
![Eaf2db13 e8db 4df7 b2ed 517fa7b4560c](https://codegenius.org/uploads/slide/image/1879/eaf2db13-e8db-4df7-b2ed-517fa7b4560c.jpeg)
ジャンプを復活させる
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Ball : MonoBehaviour
{
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinGet;
public float jumpSpeed;
private bool isJumping = false;
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);
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);
}
}
// ★★★改良(ジャンプの復活)
// OnCollisionEnterのスペルに注意
private void OnCollisionEnter(Collision other)
{
// もしも「Floor」という「Tag」がついたオブジェクトにぶつかったら(条件)
if (other.gameObject.CompareTag("Floor"))
{
// isJumpingの箱の中のデータをfalseにする。
isJumping = false;
}
}
}
![D8ade4de 08b8 4edc 8498 1ab98a0b1c9d](https://codegenius.org/uploads/slide/image/1880/d8ade4de-08b8-4edc-8498-1ab98a0b1c9d.jpeg)
![A94efb35 8e6a 43b1 9bdc c4d5e96e6e26](https://codegenius.org/uploads/slide/image/1881/a94efb35-8e6a-43b1-9bdc-c4d5e96e6e26.jpeg)
![057af899 2103 4a2d 924c 61dc13e73faf](https://codegenius.org/uploads/slide/image/1882/057af899-2103-4a2d-924c-61dc13e73faf.jpeg)
![2d808b8a 6445 4142 86eb 94f43bf32226](https://codegenius.org/uploads/slide/image/1883/2d808b8a-6445-4142-86eb-94f43bf32226.jpeg)
![25b1e330 0f1b 4ce7 b3d6 2cfc28980f83](https://codegenius.org/uploads/slide/image/1884/25b1e330-0f1b-4ce7-b3d6-2cfc28980f83.jpeg)
![140e8633 b180 4cf1 b5ec 22e863789d12](https://codegenius.org/uploads/slide/image/1885/140e8633-b180-4cf1-b5ec-22e863789d12.jpeg)
ボールをジャンプさせよう