Isomatric Tilemap을 사용해서 전체적인 필드를 구성하였습니다.
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class Player_ov : MonoBehaviour
{
private Rigidbody2D rb;
private float moveH, moveV;
private PlayerAnimation playerAnimation;
public float movespeed = 1.0f;
public TextMeshProUGUI MyScoreText;
private int ScoreNum;
// public AudioSource Soundbimyeug;
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));
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.transform.CompareTag("Treasure"))
{
ScoreNum += 1;
Destroy(other.gameObject);
MyScoreText.text = "발견한 상자 : " + ScoreNum;
}
else if (other.gameObject.transform.CompareTag("Bora"))
{
Dead();
Debug.Log("보라돌이에게 공격 당했습니다.");
}
}
public void Dead()
{
Destroy(gameObject);
}
// else if (collision.gameObject.CompareTag("Car"))
// {
// SoundCar.Play();
// }
}
}
Horizontal과 Vertical로 플레이어의 이동코드를 짰습니다.
OnTriggerEnter2D을 사용해서 상자를 먹었을 땐 스코어 텍스트에 +1을, 보라돌이에게 잡혔을 때는 플레이어 오브젝트가 삭제되게 만들었습니다.
애니메이션을 사용해서 플레이어가 이동할 때마다 여러방향으로 보이게 했습니다.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy : MonoBehaviour
{
public float speed;
public Rigidbody2D target;
bool isLive;
Rigidbody2D rigid;
SpriteRenderer spriter;
void Awake()
{
rigid = GetComponent<Rigidbody2D>();
spriter = GetComponent<SpriteRenderer>();
}
void FixedUpdate()
{
Vector2 dirVec = target.position - rigid.position;
Vector2 nextVec = dirVec.normalized * speed * Time.fixedDeltaTime;
rigid.MovePosition(rigid.position + nextVec);
rigid.velocity = Vector2.zero;
}
// void LateUpdate()
// {
// spriter.flipX = target.position.x < rigid.position.x;
// }
}
몬스터 ( 적 )이 물리엔진을 가진 플레이어의 스프라이트를 target해서 따라가게 설정했습니다.