아카이브
[개발일지] Undead Survivor 1일차 본문
플레이어 캐릭터 이동까지 구현
Player.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Player : MonoBehaviour
{
public Vector2 inputVec;
public float speed;
Rigidbody2D rbody;
// Awake는 시작할 때 한번만 실행되는 생명주기
void Awake()
{
rbody = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
//GetAxisRaw로 더욱 명확한 컨트롤 구현 가능
inputVec.x = Input.GetAxisRaw("Horizontal");
inputVec.y = Input.GetAxisRaw("Vertical");
}
//FixedUpdate: 물리 연산 프레임마다 호출되는 생명주기 함수
void FixedUpdate()
{
//위치 이동
Vector2 nextVec = inputVec.normalized * speed * Time.fixedDeltaTime;
//MovePosition은 위치이동이라 현재위치도 더해주어야 함
rbody.MovePosition(rbody.position + nextVec );
}
}
Input System 사용
패키지 매니저에서 Input System 설치
인풋 액션 설정하기
Player Input 컴포넌트 추가
Creat Actions 선택해 에셋 생성
Move 세팅
스크립트 적용
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
public Vector2 inputVec;
public float speed;
Rigidbody2D rbody;
// Awake는 시작할 때 한번만 실행되는 생명주기
void Awake()
{
rbody = GetComponent<Rigidbody2D>();
}
//FixedUpdate: 물리 연산 프레임마다 호출되는 생명주기 함수
void FixedUpdate()
{
//위치 이동
Vector2 nextVec = inputVec * speed * Time.fixedDeltaTime;
//MovePosition은 위치이동이라 현재위치도 더해주어야 함
rbody.MovePosition(rbody.position + nextVec );
}
void OnMove(InputValue value)
{
inputVec = value.Get<Vector2>();
}
}
방향 바라보기
좌측 방향키를 눌렀을 때 방향을 바꿔 바라보아야 함으로 X값이 -1 일 때 체크되어야 함
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
public Vector2 inputVec;
public float speed;
Rigidbody2D rbody;
SpriteRenderer spriter;
// Awake는 시작할 때 한번만 실행되는 생명주기
void Awake()
{
rbody = GetComponent<Rigidbody2D>();
spriter = GetComponent<SpriteRenderer>();
}
//FixedUpdate: 물리 연산 프레임마다 호출되는 생명주기 함수
void FixedUpdate()
{
//위치 이동
Vector2 nextVec = inputVec * speed * Time.fixedDeltaTime;
//MovePosition은 위치이동이라 현재위치도 더해주어야 함
rbody.MovePosition(rbody.position + nextVec );
}
void OnMove(InputValue value)
{
inputVec = value.Get<Vector2>();
}
//LateUpdate: 프레임이 종료되기 전 실행되는 생명주기 함수
void LateUpdate()
{
if (inputVec.x != 0)
{
spriter.flipX = inputVec.x < 0; //inputVec.x의 값이 0보다 작을 땐 true 클 땐 false 값이 들어감
}
}
}
애니메이션
뜯어논 스프라이트 파일로 애니메이션 만들어줌
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class Player : MonoBehaviour
{
public Vector2 inputVec;
public float speed;
Rigidbody2D rbody;
SpriteRenderer spriter;
Animator anim;
// Awake는 시작할 때 한번만 실행되는 생명주기
void Awake()
{
rbody = GetComponent<Rigidbody2D>();
spriter = GetComponent<SpriteRenderer>();
anim = GetComponent<Animator>();
}
//FixedUpdate: 물리 연산 프레임마다 호출되는 생명주기 함수
void FixedUpdate()
{
//위치 이동
Vector2 nextVec = inputVec * speed * Time.fixedDeltaTime;
//MovePosition은 위치이동이라 현재위치도 더해주어야 함
rbody.MovePosition(rbody.position + nextVec );
}
void OnMove(InputValue value)
{
inputVec = value.Get<Vector2>();
}
//LateUpdate: 프레임이 종료되기 전 실행되는 생명주기 함수
void LateUpdate()
{
anim.SetFloat("Speed", inputVec.magnitude); //magnitude: 백터의 순수 크기 값
if (inputVec.x != 0)
{
spriter.flipX = inputVec.x < 0; //inputVec.x의 값이 0보다 작을 땐 true 클 땐 false 값이 들어감
}
}
}
새로운 캐릭터로 애니메이션 만들기
'2D 콘텐츠 제작' 카테고리의 다른 글
2D 프로젝트 개발 발표 (0) | 2023.09.21 |
---|---|
[개발일지] Undead Survivor 5일차 (0) | 2023.09.19 |
[개발일지] Undead Survivor 4일차 (0) | 2023.09.19 |
[개발일지] Undead Survivor 3일차 (0) | 2023.09.15 |
[개발일지] Undead Survivor 2일차 (0) | 2023.09.14 |