using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerController : MonoBehaviour
{
public enum eControlType
{
KeyBoard, Joystick
}
private Transform tr;
[SerializeField]
private float turnSpeed = 500f;
[SerializeField]
private float moveSpeed = 5f;
[SerializeField]
private VariableJoystick joystick;
[SerializeField]
private eControlType contorlType;
// Start is called before the first frame update
void Start()
{
this.tr = this.GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
float h = 0f;
float v = 0f;
if(this.contorlType == eControlType.KeyBoard)
{
h = Input.GetAxisRaw("Horizontal"); // -1 ... 1
v = Input.GetAxisRaw("Vertical"); // -1 ... 1
}
else if (this.contorlType == 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); //축으로 돌린다
//키보드로 안누르고 있을 때
if( dir == Vector3.zero)
{
//이동
this.transform.Translate(dir * this.moveSpeed * Time.deltaTime, Space.World);
}
}
}
Input.GetMouseButton으로만 시야 회전 조절
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 contorlType;
private Animation anim;
// 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.contorlType == eControlType.KeyBoard)
{
h = Input.GetAxisRaw("Horizontal"); // -1 ... 1
v = Input.GetAxisRaw("Vertical"); // -1 ... 1
}
else if (this.contorlType == 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); //축으로 돌린다
//키보드로 안누르고 있을 때
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);
}
}
}
Down과 Up을 이용한 회전방법
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);
}
}
}
'유니티 심화' 카테고리의 다른 글
Lerp (0) | 2023.08.17 |
---|---|
카메라 세팅 (0) | 2023.08.17 |
SpaceShooter2D - 스페이스바 눌러서 총쏘기 (0) | 2023.08.17 |
절대강좌 유니티 - Chapter01 [챕터1] (0) | 2023.08.16 |
절대강좌 유니티 - SpaceShooter (0) | 2023.08.16 |