ボールをジャンプさせよう(インプットシステム)

ジャンプ
using UnityEngine;
public class Ball : MonoBehaviour
{
private InputSystem_Actions isa;
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinSound;
// ★追加(ジャンプ)
public float jumpSpeed;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
rb = GetComponent<Rigidbody>();
}
void Update()
{
Vector2 movement2 = isa.Player.Move.ReadValue<Vector2>();
Vector3 movement3 = new Vector3(movement2.x, 0, movement2.y);
rb.AddForce(movement3 * moveSpeed);
// ★追加(ジャンプ)
if (isa.Player.Jump.triggered)
{
rb.linearVelocity = Vector3.up * jumpSpeed;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinSound, transform.position);
}
}
}



空中ジャンプの禁止
using UnityEngine;
public class Ball : MonoBehaviour
{
private InputSystem_Actions isa;
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinSound;
public float jumpSpeed;
// ★★改良(空中ジャンプ禁止)
private bool isJumping = false;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
rb = GetComponent<Rigidbody>();
}
void Update()
{
Vector2 movement2 = isa.Player.Move.ReadValue<Vector2>();
Vector3 movement3 = new Vector3(movement2.x, 0, movement2.y);
rb.AddForce(movement3 * moveSpeed);
if (isa.Player.Jump.triggered && isJumping == false) // ★★改良(空中ジャンプ禁止)
{
rb.linearVelocity = Vector3.up * jumpSpeed;
// ★★改良(空中ジャンプ禁止)
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinSound, transform.position);
}
}
}


ジャンプの復活
using UnityEngine;
public class Ball : MonoBehaviour
{
private InputSystem_Actions isa;
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinSound;
public float jumpSpeed;
private bool isJumping = false;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
rb = GetComponent<Rigidbody>();
}
void Update()
{
Vector2 movement2 = isa.Player.Move.ReadValue<Vector2>();
Vector3 movement3 = new Vector3(movement2.x, 0, movement2.y);
rb.AddForce(movement3 * moveSpeed);
if (isa.Player.Jump.triggered && isJumping == false)
{
rb.linearVelocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinSound, transform.position);
}
}
// ★★★改良(ジャンプの復活)
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}



【Unity6版】BallGame(全27回)
他のコースを見る
ジャンプ
using UnityEngine;
public class Ball : MonoBehaviour
{
private InputSystem_Actions isa;
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinSound;
// ★追加(ジャンプ)
public float jumpSpeed;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
rb = GetComponent<Rigidbody>();
}
void Update()
{
Vector2 movement2 = isa.Player.Move.ReadValue<Vector2>();
Vector3 movement3 = new Vector3(movement2.x, 0, movement2.y);
rb.AddForce(movement3 * moveSpeed);
// ★追加(ジャンプ)
if (isa.Player.Jump.triggered)
{
rb.linearVelocity = Vector3.up * jumpSpeed;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinSound, transform.position);
}
}
}



空中ジャンプの禁止
using UnityEngine;
public class Ball : MonoBehaviour
{
private InputSystem_Actions isa;
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinSound;
public float jumpSpeed;
// ★★改良(空中ジャンプ禁止)
private bool isJumping = false;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
rb = GetComponent<Rigidbody>();
}
void Update()
{
Vector2 movement2 = isa.Player.Move.ReadValue<Vector2>();
Vector3 movement3 = new Vector3(movement2.x, 0, movement2.y);
rb.AddForce(movement3 * moveSpeed);
if (isa.Player.Jump.triggered && isJumping == false) // ★★改良(空中ジャンプ禁止)
{
rb.linearVelocity = Vector3.up * jumpSpeed;
// ★★改良(空中ジャンプ禁止)
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinSound, transform.position);
}
}
}


ジャンプの復活
using UnityEngine;
public class Ball : MonoBehaviour
{
private InputSystem_Actions isa;
public float moveSpeed;
private Rigidbody rb;
public AudioClip coinSound;
public float jumpSpeed;
private bool isJumping = false;
void Start()
{
isa = new InputSystem_Actions();
isa.Enable();
rb = GetComponent<Rigidbody>();
}
void Update()
{
Vector2 movement2 = isa.Player.Move.ReadValue<Vector2>();
Vector3 movement3 = new Vector3(movement2.x, 0, movement2.y);
rb.AddForce(movement3 * moveSpeed);
if (isa.Player.Jump.triggered && isJumping == false)
{
rb.linearVelocity = Vector3.up * jumpSpeed;
isJumping = true;
}
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Coin"))
{
Destroy(other.gameObject);
AudioSource.PlayClipAtPoint(coinSound, transform.position);
}
}
// ★★★改良(ジャンプの復活)
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.CompareTag("Floor"))
{
isJumping = false;
}
}
}



ボールをジャンプさせよう(インプットシステム)