Playerの作成2(Idle&Runアニメーション)












アニメーションの切り替え
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
private float moveH;
private Rigidbody2D rb2d;
// ★追加(アニメーション)
private Animator animator;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
// ★追加(アニメーション)
animator = GetComponent<Animator>();
}
void Update()
{
moveH = Input.GetAxisRaw("Horizontal");
rb2d.velocity = new Vector2(moveH * speed, rb2d.velocity.y);
// ★追加(アニメーション)
animator.SetFloat("Speed", moveH);
}
}




画像反転
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
private float moveH;
private Rigidbody2D rb2d;
private Animator animator;
// ★追加(画像反転)
private SpriteRenderer spriteRenderer;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
// ★追加(画像反転)
spriteRenderer = GetComponent<SpriteRenderer>();
}
void Update()
{
moveH = Input.GetAxisRaw("Horizontal");
rb2d.velocity = new Vector2(moveH * speed, rb2d.velocity.y);
animator.SetFloat("Speed", moveH);
// ★追加(画像反転)
if (moveH > 0.5f)
{
spriteRenderer.flipX = false;
}
else if (moveH < -0.5f)
{
spriteRenderer.flipX = true;
}
}
}

【2022版】DarkCastle(全39回)
他のコースを見る











アニメーションの切り替え
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
private float moveH;
private Rigidbody2D rb2d;
// ★追加(アニメーション)
private Animator animator;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
// ★追加(アニメーション)
animator = GetComponent<Animator>();
}
void Update()
{
moveH = Input.GetAxisRaw("Horizontal");
rb2d.velocity = new Vector2(moveH * speed, rb2d.velocity.y);
// ★追加(アニメーション)
animator.SetFloat("Speed", moveH);
}
}




画像反転
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
private float moveH;
private Rigidbody2D rb2d;
private Animator animator;
// ★追加(画像反転)
private SpriteRenderer spriteRenderer;
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
// ★追加(画像反転)
spriteRenderer = GetComponent<SpriteRenderer>();
}
void Update()
{
moveH = Input.GetAxisRaw("Horizontal");
rb2d.velocity = new Vector2(moveH * speed, rb2d.velocity.y);
animator.SetFloat("Speed", moveH);
// ★追加(画像反転)
if (moveH > 0.5f)
{
spriteRenderer.flipX = false;
}
else if (moveH < -0.5f)
{
spriteRenderer.flipX = true;
}
}
}

Playerの作成2(Idle&Runアニメーション)