Playerの作成5(Jumpアニメーション)











ジャンプアニメーション
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);
// ★追加(ジャンプアニメーション)
animator.SetTrigger("Jump");
}
}
}

【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);
// ★追加(ジャンプアニメーション)
animator.SetTrigger("Jump");
}
}
}

Playerの作成5(Jumpアニメーション)