Playerの作成3(Runアニメーション)
移動アニメーション
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
// ★追加(移動アニメーション)
private Animator animator;
void Start()
{
// ★追加(移動アニメーション)
animator = GetComponent<Animator>();
}
void Update()
{
float moveH = Input.GetAxis("Horizontal");
Vector2 movement = new Vector2(moveH, 0);
transform.Translate(movement * Time.deltaTime * speed);
// ★追加(移動アニメーション)
animator.SetFloat("Speed", moveH);
}
}
画像反転
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
private Animator animator;
// ★追加(画像反転)
private SpriteRenderer spriteRenderer;
void Start()
{
animator = GetComponent<Animator>();
// ★追加(画像反転)
spriteRenderer = GetComponent<SpriteRenderer>();
}
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;
}
}
}
【2022版】ActionGame2D(全33回)
他のコースを見る移動アニメーション
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
// ★追加(移動アニメーション)
private Animator animator;
void Start()
{
// ★追加(移動アニメーション)
animator = GetComponent<Animator>();
}
void Update()
{
float moveH = Input.GetAxis("Horizontal");
Vector2 movement = new Vector2(moveH, 0);
transform.Translate(movement * Time.deltaTime * speed);
// ★追加(移動アニメーション)
animator.SetFloat("Speed", moveH);
}
}
画像反転
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float speed;
private Animator animator;
// ★追加(画像反転)
private SpriteRenderer spriteRenderer;
void Start()
{
animator = GetComponent<Animator>();
// ★追加(画像反転)
spriteRenderer = GetComponent<SpriteRenderer>();
}
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;
}
}
}
Playerの作成3(Runアニメーション)