ボールをジャンプさせよう
![3def7538 a445 48ef aba5 776f731b3148](https://codegenius.org/uploads/slide/image/5690/3def7538-a445-48ef-aba5-776f731b3148.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);
}
}
}
![4dedbcb9 f9c2 4f2d 8be6 5be507f20169](https://codegenius.org/uploads/slide/image/5691/4dedbcb9-f9c2-4f2d-8be6-5be507f20169.jpeg)
![Cd466acd 2b5c 4688 b1c4 9050cf7e09a2](https://codegenius.org/uploads/slide/image/5692/cd466acd-2b5c-4688-b1c4-9050cf7e09a2.jpeg)
![464df080 661f 4dad 8200 e187ab7bb2b7](https://codegenius.org/uploads/slide/image/5693/464df080-661f-4dad-8200-e187ab7bb2b7.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);
}
}
}
![C94cf947 a52b 4f46 8ef1 beb831e1d240](https://codegenius.org/uploads/slide/image/5694/c94cf947-a52b-4f46-8ef1-beb831e1d240.jpeg)
![090b2d51 fa89 4c9e b2fa 2785fe376d47](https://codegenius.org/uploads/slide/image/5695/090b2d51-fa89-4c9e-b2fa-2785fe376d47.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 collision)
{
if(collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![9cf1bfe1 d364 4d61 aef0 79cf71be09ce](https://codegenius.org/uploads/slide/image/5696/9cf1bfe1-d364-4d61-aef0-79cf71be09ce.jpeg)
![1fe125b1 37ec 435d ab06 e732c21abfdf](https://codegenius.org/uploads/slide/image/5697/1fe125b1-37ec-435d-ab06-e732c21abfdf.jpeg)
![1bf4b1d2 11b2 4021 9b67 5851178655c6](https://codegenius.org/uploads/slide/image/5698/1bf4b1d2-11b2-4021-9b67-5851178655c6.jpeg)
![5aaa3e8e 10a7 4c6b a6ac 285b4ac914fe](https://codegenius.org/uploads/slide/image/5699/5aaa3e8e-10a7-4c6b-a6ac-285b4ac914fe.jpeg)
![721a993b 4611 4c75 bb06 aeae81d788bd](https://codegenius.org/uploads/slide/image/5700/721a993b-4611-4c75-bb06-aeae81d788bd.jpeg)
![Cc381e30 f7e1 4f7a a726 262e970a3ad8](https://codegenius.org/uploads/slide/image/5701/cc381e30-f7e1-4f7a-a726-262e970a3ad8.jpeg)
![D4f1d014 270e 433e 8ed0 acd822052ae0](https://codegenius.org/uploads/slide/image/5702/d4f1d014-270e-433e-8ed0-acd822052ae0.jpeg)
【2020版】BallGame(全27回)
他のコースを見る![3def7538 a445 48ef aba5 776f731b3148](https://codegenius.org/uploads/slide/image/5690/3def7538-a445-48ef-aba5-776f731b3148.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);
}
}
}
![4dedbcb9 f9c2 4f2d 8be6 5be507f20169](https://codegenius.org/uploads/slide/image/5691/4dedbcb9-f9c2-4f2d-8be6-5be507f20169.jpeg)
![Cd466acd 2b5c 4688 b1c4 9050cf7e09a2](https://codegenius.org/uploads/slide/image/5692/cd466acd-2b5c-4688-b1c4-9050cf7e09a2.jpeg)
![464df080 661f 4dad 8200 e187ab7bb2b7](https://codegenius.org/uploads/slide/image/5693/464df080-661f-4dad-8200-e187ab7bb2b7.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);
}
}
}
![C94cf947 a52b 4f46 8ef1 beb831e1d240](https://codegenius.org/uploads/slide/image/5694/c94cf947-a52b-4f46-8ef1-beb831e1d240.jpeg)
![090b2d51 fa89 4c9e b2fa 2785fe376d47](https://codegenius.org/uploads/slide/image/5695/090b2d51-fa89-4c9e-b2fa-2785fe376d47.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 collision)
{
if(collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}
![9cf1bfe1 d364 4d61 aef0 79cf71be09ce](https://codegenius.org/uploads/slide/image/5696/9cf1bfe1-d364-4d61-aef0-79cf71be09ce.jpeg)
![1fe125b1 37ec 435d ab06 e732c21abfdf](https://codegenius.org/uploads/slide/image/5697/1fe125b1-37ec-435d-ab06-e732c21abfdf.jpeg)
![1bf4b1d2 11b2 4021 9b67 5851178655c6](https://codegenius.org/uploads/slide/image/5698/1bf4b1d2-11b2-4021-9b67-5851178655c6.jpeg)
![5aaa3e8e 10a7 4c6b a6ac 285b4ac914fe](https://codegenius.org/uploads/slide/image/5699/5aaa3e8e-10a7-4c6b-a6ac-285b4ac914fe.jpeg)
![721a993b 4611 4c75 bb06 aeae81d788bd](https://codegenius.org/uploads/slide/image/5700/721a993b-4611-4c75-bb06-aeae81d788bd.jpeg)
![Cc381e30 f7e1 4f7a a726 262e970a3ad8](https://codegenius.org/uploads/slide/image/5701/cc381e30-f7e1-4f7a-a726-262e970a3ad8.jpeg)
![D4f1d014 270e 433e 8ed0 acd822052ae0](https://codegenius.org/uploads/slide/image/5702/d4f1d014-270e-433e-8ed0-acd822052ae0.jpeg)
ボールをジャンプさせよう