using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MoveCamera : MonoBehaviour
{
[SerializeField] private GameObject player;
private float playerPreviousX;
void Start()
{
playerPreviousX = player.transform.position.x;
}
void Update()
{
// 플레이어의 x 위치가 0보다 작으면
if (player.transform.position.x < 0)
{
if (player.transform.position.x < -6)
{
this.transform.position = this.transform.position;
return;
}
float xDifference = player.transform.position.x - playerPreviousX; // 이동한 x 거리를 계산
// 메인카메라의 위치를 플레이어의 이동량만큼 수정
this.transform.position = new Vector3(this.transform.position.x + xDifference,
this.transform.position.y,
this.transform.position.z);
}
// 이전 플레이어 x 위치를 현재 위치로 업데이트
playerPreviousX = player.transform.position.x;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public Joystick joystick;
public float moveSpeed = 5.0f; // Player의 이동 속도
private AnimationState currentState;
private Animator animator;
void Start()
{
// 시작 시 애니메이션 상태를 Idle로 설정
currentState = AnimationState.Idle;
animator = GetComponent<Animator>();
}
void Update()
{
Move();
HandleAnimation();
}
void Move()
{
// Joystick의 입력에 따른 방향을 설정
// x축의 -방향이 앞쪽, z축의 +방향이 오른쪽
Vector3 moveDirection = new Vector3(-joystick.Vertical, 0, joystick.Horizontal).normalized;
transform.Translate(moveDirection * moveSpeed * Time.deltaTime, Space.World);
// 움직이는 방향에 따라 Player를 회전
if (moveDirection != Vector3.zero)
{
Quaternion toRotation = Quaternion.LookRotation(moveDirection, Vector3.up);
transform.rotation = Quaternion.RotateTowards(transform.rotation, toRotation, 360f * Time.deltaTime);
currentState = AnimationState.Run;
}
else
{
currentState = AnimationState.Idle;
}
}
void HandleAnimation()
{
// 애니메이터의 "state" 파라미터를 현재 애니메이션 상태로 설정
animator.SetInteger("state", (int)currentState);
}
}
public enum AnimationState
{
Idle = 0,
Run = 1
}
'KDT > 3D 콘텐츠 제작' 카테고리의 다른 글
[HeroShooter] 벽(콜라이더)에 부딫히면 떨리는 문제 해결 (0) | 2023.08.29 |
---|---|
[HeroShooter] 스테이지2~3: 움직이지 않고 원거리 공격을 하는 몬스터 구현 (1) | 2023.08.27 |
[HeroShooter] Stage2. Enemy의 피격과 사망, 인식 구현 (0) | 2023.08.24 |
[HeroShooter] Ray와 Overlap을 이용한 적 감지 구현 (0) | 2023.08.23 |
[HeroShooter] Tutorial UI 및 트리거 구현 (0) | 2023.08.22 |