728x90
1. 프리펩 만들기
2. Animator controller
3. 애니메이션 복제
4. 애니메이션 0:00 삭제
5. Has 끄기
6. 파라미터
스피드값
Run->Stand Less 0.1
Stand -> Greater 0.1
7. 바닥
8. Rig Cap 컴포넌트 추가
9. 바닥 위치값 출력
C# Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchMovement : MonoBehaviour
{
private Vector3 moveTargetPos;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//터치 또는 마우스 왼쪽 버튼을 클릭업 했을때
if (Input.GetMouseButtonUp(0))
{
// 마우스 클릭 또는 터치 위치의 레이를 구함
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//충돌 정보 구조체
RaycastHit hitInfo;
//생성된 10000f 길이의 레이를 통해 충돌 정보를 hitInfo로 저장받음(out)
if (Physics.Raycast(ray, out hitInfo, 10000f))
{
moveTargetPos = hitInfo.point;
Debug.Log("이동할 바닥의 위치값 : " + moveTargetPos);
}
}
}
}
10. 레이추가
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchMovement : MonoBehaviour
{
private Vector3 moveTargetPos;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//터치 또는 마우스 왼쪽 버튼을 클릭업 했을때
if (Input.GetMouseButtonUp(0))
{
// 마우스 클릭 또는 터치 위치의 레이를 구함
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Debug.DrawRay : Ray를 그려주는 메소드
//-> Debug.DrawRay(생성위치, 레이방향 * 길이, 색상, 시간);
Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.red, 1f);
//충돌 정보 구조체
RaycastHit hitInfo;
//생성된 10000f 길이의 레이를 통해 충돌 정보를 hitInfo로 저장받음(out)
if (Physics.Raycast(ray, out hitInfo, 10000f))
{
moveTargetPos = hitInfo.point;
Debug.Log("이동할 바닥의 위치값 : " + moveTargetPos);
}
}
}
}
11. 캐릭터이동
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchMovement : MonoBehaviour
{
private Vector3 moveTargetPos;
public float speed;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//터치 또는 마우스 왼쪽 버튼을 클릭업 했을때
if (Input.GetMouseButtonUp(0))
{
// 마우스 클릭 또는 터치 위치의 레이를 구함
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Debug.DrawRay : Ray를 그려주는 메소드
//-> Debug.DrawRay(생성위치, 레이방향 * 길이, 색상, 시간);
Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.red, 1f);
//충돌 정보 구조체
RaycastHit hitInfo;
//생성된 10000f 길이의 레이를 통해 충돌 정보를 hitInfo로 저장받음(out)
if (Physics.Raycast(ray, out hitInfo, 10000f))
{
moveTargetPos = hitInfo.point;
Debug.Log("이동할 바닥의 위치값 : " + moveTargetPos);
}
}
//캐릭터 이동 처리
transform.position = Vector3.MoveTowards(transform.position, moveTargetPos, speed * Time.deltaTime);
}
}
12. 추적카메라 만들기
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CameraFollow : MonoBehaviour
{
public Transform target; //추적 대상 Transform 참조
//추적시 이동 보간값
public float smoothing = 5f;
Vector3 offset; //카메라와 캐릭터간의 거리
// Start is called before the first frame update
void Start()
{
//캐릭터와 카메라간의 거리를 계산
offset = transform.position - target.position;
}
//
// Update is called once per frame
void Update()
{
//카메라가 offset 간격을 유지하며 추적
//카메라 위치 계산
Vector3 targetCamPos = target.position + offset;
//카메라의 위치를 Lerp(선형보간)을 적용하여 이동함
transform.position = Vector3.Lerp(transform.position, targetCamPos, smoothing * Time.deltaTime);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TouchMovement : MonoBehaviour
{
//이동하려는 위치
private Vector3 moveTargetPos;
//이동속도
public float speed;
//애니메이터
public Animator animator;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
//터치 또는 마우스 왼쪽 버튼을 클릭업 했을때
if (Input.GetMouseButtonUp(0))
{
// 마우스 클릭 또는 터치 위치의 레이를 구함
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// Debug.DrawRay : Ray를 그려주는 메소드
//-> Debug.DrawRay(생성위치, 레이방향 * 길이, 색상, 시간);
Debug.DrawRay(ray.origin, ray.direction * 1000f, Color.red, 1f);
//충돌 정보 구조체
RaycastHit hitInfo;
//생성된 10000f 길이의 레이를 통해 충돌 정보를 hitInfo로 저장받음(out)
if (Physics.Raycast(ray, out hitInfo, 10000f))
{
moveTargetPos = hitInfo.point;
Debug.Log("이동할 바닥의 위치값 : " + moveTargetPos);
}
}
//캐릭터와 이동 위치간의 거리
//float 거리
float dis = Vector3.Distance(transform.position, moveTargetPos);
if (dis >= 0.01f)
{
//캐릭터 이동 처리
transform.position = Vector3.MoveTowards(transform.position, moveTargetPos, speed * Time.deltaTime);
//이동 애니메이션 변경
animator.SetFloat("speed", speed);
//이동방향
Vector3 direction = moveTargetPos - transform.position;
//이동 방향 정규화 벡터
Vector3 dirNormalZ = new Vector3(direction.x, 0f, direction.z).normalized;
//이동방향 벡터를 이용한 회전(쿼터니언)을 구함
//Quaternion.LookRotation(이동방향벡터);
Quaternion targetRot = Quaternion.LookRotation(dirNormalZ);
transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRot, 550.0f * Time.deltaTime);
}
else
{
animator.SetFloat("speed", 0f);
}
}
}
'Unity' 카테고리의 다른 글
[유니티] 조명설정하기 (0) | 2021.05.14 |
---|---|
[유니티] 회전하기 (0) | 2021.05.14 |
[유니티] 효과 넣기 (0) | 2021.05.06 |
[유니티] 3D 점수 계산 (0) | 2021.05.06 |
[유니티] 지정위치에 랜덤 오브젝트 놓기 (0) | 2021.04.24 |
댓글