PlayerController
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public enum eControlType
{
KeyBoard, Joystick
}
public enum eAnimState {
Idle, RunB, RunF, RunL, RunR
}
private Transform tr;
[SerializeField]
private float turnSpeed = 500f;
[SerializeField]
private float moveSpeed = 5f;
[SerializeField]
private VariableJoystick joystick;
[SerializeField]
private eControlType controlType;
private Animation anim;
private Vector3 downPosition;
private bool isDown = false;
// Start is called before the first frame update
void Start()
{
this.anim = this.GetComponent<Animation>();
this.tr = this.GetComponent<Transform>();
//this.anim.Play(eAnimState.Idle.ToString());
this.anim.clip = this.anim.GetClip(eAnimState.Idle.ToString());
this.anim.Play();
}
// Update is called once per frame
void Update()
{
float h = 0f;
float v = 0f;
if(this.controlType == eControlType.KeyBoard)
{
h = Input.GetAxisRaw("Horizontal"); // -1 ... 1
v = Input.GetAxisRaw("Vertical"); // -1 ... 1
}
else if (this.controlType == eControlType.Joystick)
{
h = this.joystick.Direction.x;
v = this.joystick.Direction.y;
}
Vector3 dir = new Vector3(h, 0, v);
//float angle = Mathf.Atan2(dir.x, dir.z) * Mathf.Rad2Deg; // 두 점사이의 각도 라디안을 구한다, 앵글을 구한다
//this.transform.localRotation = Quaternion.AngleAxis(angle, Vector3.up); //축으로 돌린다
var r = Input.GetAxis("Mouse X");
////드래그 할 때 이동
if (Input.GetMouseButton(0))
{
// 축 방향 속도 시간
this.transform.Rotate(Vector3.up * r * Time.deltaTime * this.turnSpeed);
}
//if(Input.GetMouseButtonDown(0))
//{
// Debug.Log("Down");
// this.isDown = true;
// this.downPosition = Input.mousePosition;
//}
//else if (Input.GetMouseButtonUp(0))
//{
// Debug.Log("UP");
// this.isDown = false;
//}
//if(this.isDown)
//{
// if(this.downPosition != Input.mousePosition)
// {
// var rotDir = Mathf.Sign(r);
// this.transform.Rotate(Vector3.up * rotDir * Time.deltaTime * this.turnSpeed);
// this.downPosition = Input.mousePosition;
// }
//}
//키보드로 안누르고 있을 때
if ( dir != Vector3.zero)
{
//이동
this.transform.Translate(dir * this.moveSpeed * Time.deltaTime, Space.World);
}
this.PlayAnimation(dir);
}
private void PlayAnimation(Vector3 dir)
{
//오른쪽
if(dir.x > 0)
{
this.anim.CrossFade(eAnimState.RunR.ToString(), 0.25f);
}
else if (dir.x < 0) // 왼쪽
{
this.anim.CrossFade(eAnimState.RunL.ToString(), 0.25f);
}
else if (dir.z > 0) //앞
{
this.anim.CrossFade(eAnimState.RunF.ToString(), 0.25f);
}
else if (dir.z < 0) // 뒤
{
this.anim.CrossFade(eAnimState.RunB.ToString(), 0.25f);
}
else //Idle
{
this.anim.CrossFade(eAnimState.Idle.ToString(), 0.25f);
}
}
}
FollowCam
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FollowCam : MonoBehaviour
{
[SerializeField]
private Transform headTrans;
[SerializeField]
private Transform shoesTrans;
[SerializeField]
private Transform point;
[SerializeField]
private float distance = 10.0f;
[SerializeField]
private float height = 2.0f;
[SerializeField]
private Transform startTrans;
[SerializeField]
private Transform endTrans;
[SerializeField]
[Range(0,1)]
private float lerpValue;
private Vector3 tpos;
// Start is called before the first frame update
void Start()
{
//시작 위치
this.tpos = this.shoesTrans.position + this.shoesTrans.forward * -1 * this.distance;
this.tpos = this.tpos + Vector3.up * height;
this.transform.position = this.tpos;
}
//Update메서드 이후
private void LateUpdate()
{
float wheel = Input.GetAxis("Mouse ScrollWheel");
Vector3 lerpTpos = Vector3.Lerp(this.shoesTrans.position, this.headTrans.position, this.lerpValue);
this.startTrans.position = this.transform.position + this.transform.forward * 3f;
this.endTrans.position = this.transform.position + -this.transform.forward * 3f;
if(wheel > 0)
{
this.transform.position = this.transform.position + this.transform.forward * 0.1f;
}
else if(wheel < 0)
{
this.transform.position = this.transform.position + -this.transform.forward * 0.1f;
}
this.transform.LookAt(lerpTpos);
}
}
'유니티 심화' 카테고리의 다른 글
절대강좌 유니티 - 카메라 distance, height 이동 (0) | 2023.08.18 |
---|---|
Vector3.normalized (0) | 2023.08.18 |
Vector (0) | 2023.08.17 |
잘못만든 카메라 세팅 (0) | 2023.08.17 |
Lerp (0) | 2023.08.17 |