Playerの作成4(ジャンプ)

ジャンプ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
private Animator animator;
private SpriteRenderer spriteRenderer;
// ★追加(ジャンプ)
public float jumpSpeed;
public AudioClip jumpSound;
private Rigidbody2D rb2d;
private AudioSource audioSource;
void Start()
{
animator = GetComponent<Animator>();
spriteRenderer = GetComponent<SpriteRenderer>();
// ★追加(ジャンプ)
rb2d = GetComponent<Rigidbody2D>();
audioSource = GetComponent<AudioSource>();
}
void Update()
{
float moveH = Input.GetAxisRaw("Horizontal");
Vector2 movement = new Vector2(moveH, 0);
transform.Translate(movement * Time.deltaTime * speed);
animator.SetFloat("Speed", moveH);
if (moveH > 0.5f)
{
spriteRenderer.flipX = false;
}
else if (moveH < -0.5f)
{
spriteRenderer.flipX = true;
}
// ★追加(ジャンプ)
if(Input.GetKeyDown(KeyCode.Space))
{
rb2d.velocity = Vector2.up * jumpSpeed;
audioSource.PlayOneShot(jumpSound);
}
}
}








【2022版】ActionGame2D(全33回)
他のコースを見る
ジャンプ
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
private Animator animator;
private SpriteRenderer spriteRenderer;
// ★追加(ジャンプ)
public float jumpSpeed;
public AudioClip jumpSound;
private Rigidbody2D rb2d;
private AudioSource audioSource;
void Start()
{
animator = GetComponent<Animator>();
spriteRenderer = GetComponent<SpriteRenderer>();
// ★追加(ジャンプ)
rb2d = GetComponent<Rigidbody2D>();
audioSource = GetComponent<AudioSource>();
}
void Update()
{
float moveH = Input.GetAxisRaw("Horizontal");
Vector2 movement = new Vector2(moveH, 0);
transform.Translate(movement * Time.deltaTime * speed);
animator.SetFloat("Speed", moveH);
if (moveH > 0.5f)
{
spriteRenderer.flipX = false;
}
else if (moveH < -0.5f)
{
spriteRenderer.flipX = true;
}
// ★追加(ジャンプ)
if(Input.GetKeyDown(KeyCode.Space))
{
rb2d.velocity = Vector2.up * jumpSpeed;
audioSource.PlayOneShot(jumpSound);
}
}
}








Playerの作成4(ジャンプ)