ボールをジャンプさせよう
![Aa039d88 022e 4870 9332 14953ea953d0](https://codegenius.org/uploads/slide/image/9539/aa039d88-022e-4870-9332-14953ea953d0.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);
}
}
}
![Fccd8db4 98e7 4744 ba4c d70988058316](https://codegenius.org/uploads/slide/image/9540/fccd8db4-98e7-4744-ba4c-d70988058316.jpeg)
![0a764182 8fe5 43e0 8bd4 97aa13dd8666](https://codegenius.org/uploads/slide/image/9541/0a764182-8fe5-43e0-8bd4-97aa13dd8666.jpeg)
![96ae694a c9ef 4d60 8852 de56bfbd0f17](https://codegenius.org/uploads/slide/image/9542/96ae694a-c9ef-4d60-8852-de56bfbd0f17.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);
}
}
}
![D7efa3b9 3155 4ecc 97b1 73ad1252ab28](https://codegenius.org/uploads/slide/image/9543/d7efa3b9-3155-4ecc-97b1-73ad1252ab28.jpeg)
![6f80cba1 c02f 4e32 a85d 73134d1c08d3](https://codegenius.org/uploads/slide/image/9544/6f80cba1-c02f-4e32-a85d-73134d1c08d3.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;
}
}
}
![103c85e8 7b1d 41d5 90b8 5e8b8edbb454](https://codegenius.org/uploads/slide/image/9545/103c85e8-7b1d-41d5-90b8-5e8b8edbb454.jpeg)
![D0579a6e e76b 469a aa85 042281039195](https://codegenius.org/uploads/slide/image/9546/d0579a6e-e76b-469a-aa85-042281039195.jpeg)
![3167e2b6 2fda 4ddb 8ba2 a6d837eeab8d](https://codegenius.org/uploads/slide/image/9547/3167e2b6-2fda-4ddb-8ba2-a6d837eeab8d.jpeg)
![A544af09 afa8 4005 86f4 f71043950622](https://codegenius.org/uploads/slide/image/9548/a544af09-afa8-4005-86f4-f71043950622.jpeg)
![D4e824d3 0352 4553 b490 6e11e071cce3](https://codegenius.org/uploads/slide/image/9549/d4e824d3-0352-4553-b490-6e11e071cce3.jpeg)
![A49b5fb7 9fbf 4e03 9898 4b3c3e41dfdc](https://codegenius.org/uploads/slide/image/9550/a49b5fb7-9fbf-4e03-9898-4b3c3e41dfdc.jpeg)
![77529f80 e004 4987 8c77 7bc361ad6cd2](https://codegenius.org/uploads/slide/image/9551/77529f80-e004-4987-8c77-7bc361ad6cd2.jpeg)
【2022版】BallGame(全27回)
他のコースを見る![Aa039d88 022e 4870 9332 14953ea953d0](https://codegenius.org/uploads/slide/image/9539/aa039d88-022e-4870-9332-14953ea953d0.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);
}
}
}
![Fccd8db4 98e7 4744 ba4c d70988058316](https://codegenius.org/uploads/slide/image/9540/fccd8db4-98e7-4744-ba4c-d70988058316.jpeg)
![0a764182 8fe5 43e0 8bd4 97aa13dd8666](https://codegenius.org/uploads/slide/image/9541/0a764182-8fe5-43e0-8bd4-97aa13dd8666.jpeg)
![96ae694a c9ef 4d60 8852 de56bfbd0f17](https://codegenius.org/uploads/slide/image/9542/96ae694a-c9ef-4d60-8852-de56bfbd0f17.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);
}
}
}
![D7efa3b9 3155 4ecc 97b1 73ad1252ab28](https://codegenius.org/uploads/slide/image/9543/d7efa3b9-3155-4ecc-97b1-73ad1252ab28.jpeg)
![6f80cba1 c02f 4e32 a85d 73134d1c08d3](https://codegenius.org/uploads/slide/image/9544/6f80cba1-c02f-4e32-a85d-73134d1c08d3.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;
}
}
}
![103c85e8 7b1d 41d5 90b8 5e8b8edbb454](https://codegenius.org/uploads/slide/image/9545/103c85e8-7b1d-41d5-90b8-5e8b8edbb454.jpeg)
![D0579a6e e76b 469a aa85 042281039195](https://codegenius.org/uploads/slide/image/9546/d0579a6e-e76b-469a-aa85-042281039195.jpeg)
![3167e2b6 2fda 4ddb 8ba2 a6d837eeab8d](https://codegenius.org/uploads/slide/image/9547/3167e2b6-2fda-4ddb-8ba2-a6d837eeab8d.jpeg)
![A544af09 afa8 4005 86f4 f71043950622](https://codegenius.org/uploads/slide/image/9548/a544af09-afa8-4005-86f4-f71043950622.jpeg)
![D4e824d3 0352 4553 b490 6e11e071cce3](https://codegenius.org/uploads/slide/image/9549/d4e824d3-0352-4553-b490-6e11e071cce3.jpeg)
![A49b5fb7 9fbf 4e03 9898 4b3c3e41dfdc](https://codegenius.org/uploads/slide/image/9550/a49b5fb7-9fbf-4e03-9898-4b3c3e41dfdc.jpeg)
![77529f80 e004 4987 8c77 7bc361ad6cd2](https://codegenius.org/uploads/slide/image/9551/77529f80-e004-4987-8c77-7bc361ad6cd2.jpeg)
ボールをジャンプさせよう