총 3가지의 구성으로 맵을 만들었습니다.
위에 있는 타일들을 타일맵으로 활용해 맵을 만들었습니다.
완성된 맵입니다.
애니메이션을 구현하였습니다.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
private Rigidbody2D rb;
private float moveH, moveV;
private PlayerAnimation playerAnimation;
public float moveSpeed = 3.0f;
private void Awake()
{
rb = GetComponent<Rigidbody2D>();
playerAnimation = FindObjectOfType<PlayerAnimation>();
}
private void Update()
{
moveH = Input.GetAxis("Horizontal");
moveV = Input.GetAxis("Vertical");
}
private void FixedUpdate()
{
Vector2 currentPos = rb.position;
Vector2 inputVector = new Vector2(moveH, moveV).normalized * moveSpeed * Time.fixedDeltaTime;
rb.MovePosition(currentPos + inputVector);
playerAnimation.SetDirection(new Vector2(moveH,moveV));
}
}
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerAnimation : MonoBehaviour
{
private Animator anim;
private int lastDir;
public string[] staticDirections =
{ "Static N", "Static NW", "Static W", "Static SW", "Static S", "Static SE", "Static E", "Static NE" };
public string[] runDirections =
{ "Run N", "Run NW", "Run W", "Run SW", "Run S", "Run SE", "Run E", "Run NE" };
private void Awake()
{
anim = GetComponent<Animator>();
}
public void SetDirection(Vector2 direction)
{
if (direction.magnitude < 0.01)
{
anim.Play(staticDirections[lastDir]);
}
else
{
lastDir = DirectionToIndex(direction);
anim.Play(runDirections[lastDir]);
}
}
private int DirectionToIndex(Vector2 _direction)
{
Vector2 norDir = _direction.normalized;
float step = 360 / 8;
float offset = step / 2;
float angle = Vector2.SignedAngle(Vector2.up, norDir);
angle += offset;
if (angle < 0)
{
angle += 360;
}
float stepCount = angle / step;
return Mathf.FloorToInt(stepCount);
}
}
플레이어를 움직이게하는 코드입니다.