게임클라이언트 프로그래밍/Unity
메카님 연습
Game Client Lee Hwanguk
2023. 2. 1. 23:35
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cucumber : MonoBehaviour
{
private GameObject cucumber;
private float speed = 1f;
// Start is called before the first frame update
void Start()
{
this.cucumber = GetComponent<GameObject>();
}
// Update is called once per frame
void Update()
{
//입력받으면? x방향으로 이동(방향 값을 알수있다)
float dirx = 0;
if (Input.GetKey(KeyCode.RightArrow)) dirx = 1;
if (Input.GetKey(KeyCode.LeftArrow)) dirx = -1;
//방향*속도*시간
this.transform.Translate(new Vector3(dirx, 0, 0)*this.speed*Time.deltaTime);
if (dirx != 0) this.transform.localScale = new Vector3(dirx, 1, 1); //dir값이 1또눈 -1,이미지 반전시키기
}
}
#cucumber가 이동을 하면 이미지를 반전시켜야하는데 생각하는 방향과 반대로 회전을 한다
#엑세스 시킨 원본 이미지가 처음부터 왼쪽을 바라보고있었다
#x축의 filp을 체크해주고 문제를 해결
#이미지의 처음 방향을 생각하고 구현해야한다
#Mathf.Clamp(value,min,max) -value값의 최소,최대값 범위 지정
var clamp = Mathf.Clamp(this.transform.position.x, LeftBound, RightBound);
var pos=this.transform.position;
pos.x = clamp;
this.transform.position = pos;
//Debug.Log(clamp);
#애니메이션 전환(SetInteger("State",1);)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cucumber : MonoBehaviour
{
private GameObject cucumber;
private float speed = 1f;
private float RightBound=0.82f; //오른쪽 화면 경계
private float LeftBound=-0.82f; //왼쪽 화면 경계
private Animator anim;
// Start is called before the first frame update
void Start()
{
this.cucumber = GetComponent<GameObject>();
this.anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
//입력받으면? x방향으로 이동(방향 값을 알수있다)
//Run 애니매이션으로 전환
//dir의 경우의수 -1,0,1 (dirx==-1 ,1이면 Run으로 전환)
float dirx = 0;
if (Input.GetKey(KeyCode.RightArrow))
{
dirx = 1;
Debug.Log(this.anim); //null
this.anim.SetInteger("State", 1);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
dirx = -1;
Debug.Log(this.anim); //null
this.anim.SetInteger("State", 1);
}
//방향*속도*시간
this.transform.Translate(new Vector3(dirx, 0, 0)*this.speed*Time.deltaTime);
//dir값이 1또눈 -1,이미지 반전시키기
if (dirx != 0) this.transform.localScale = new Vector3(dirx, 1, 1);
//Debug.Log(this.transform.position.x);
//이동 제한하기(Lefrbound/Rightbound)
var clamp = Mathf.Clamp(this.transform.position.x, LeftBound, RightBound);
var pos=this.transform.position;
pos.x = clamp;
this.transform.position = pos;
//Debug.Log(clamp);
}
}
#어느 조건이 만족되면? 어디서 전환할지?
#Animator를 먼저 구상하고 스크립트를 작성하자
#순서가 꼬이면 코드가 꼬인다 ...
float dirx = 0;
if (Input.GetKey(KeyCode.RightArrow)) //Run
{
dirx = 1;
Debug.Log(this.anim); //null
this.anim.SetInteger("State", 1);
this.dustGo.SetActive(true);
this.state=eState.Run;
}
if (Input.GetKey(KeyCode.LeftArrow)) //Run
{
dirx = -1;
Debug.Log(this.anim); //null
this.anim.SetInteger("State", 1);
this.dustGo.SetActive(true);
this.state = eState.Run;
}
else if (dirx == 0) //Idle
{
if(this.state!=eState.Attack)
{
this.anim.SetInteger("State", 0);
this.dustGo.SetActive(false);
this.state = eState.Idle;
Debug.Log("Idle");
}
}
#Run상태일때 Dust가 활성(SetActive==true) Idle일때는 비활성(SetActive==false)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Cucumber : MonoBehaviour
{
public enum eState
{
None=-1,
Idle, //0
Run, //1
Attack //2
}
private GameObject cucumber;
private float speed = 1f;
private float RightBound=0.82f; //오른쪽 화면 경계
private float LeftBound=-0.82f; //왼쪽 화면 경계
private Animator anim;
[SerializeField]
private GameObject dustGo;
private eState state;
private float delta;
// Start is called before the first frame update
void Start()
{
this.cucumber = GetComponent<GameObject>();
this.anim = GetComponent<Animator>();
this.dustGo.SetActive(false);
this.state=eState.Idle; //시작 idle
}
// Update is called once per frame
void Update()
{
//입력받으면? x방향으로 이동(방향 값을 알수있다)
//Run 애니매이션으로 전환
//dir의 경우의수 -1,0,1 (dirx==-1 ,1이면 Run으로 전환)
float dirx = 0;
if (Input.GetKey(KeyCode.RightArrow)) //Run
{
dirx = 1;
Debug.Log(this.anim); //null
this.anim.SetInteger("State", 1);
this.dustGo.SetActive(true);
this.state=eState.Run;
}
if (Input.GetKey(KeyCode.LeftArrow)) //Run
{
dirx = -1;
Debug.Log(this.anim); //null
this.anim.SetInteger("State", 1);
this.dustGo.SetActive(true);
this.state = eState.Run;
}
else if (dirx == 0) //Idle
{
if(this.state!=eState.Attack)
{
this.anim.SetInteger("State", 0);
this.dustGo.SetActive(false);
this.state = eState.Idle;
Debug.Log("Idle");
}
}
//방향*속도*시간
this.transform.Translate(new Vector3(dirx, 0, 0)*this.speed*Time.deltaTime);
//dir값이 1또눈 -1,이미지 반전시키기
if (dirx != 0) this.transform.localScale = new Vector3(dirx, 1, 1);
//Debug.Log(this.transform.position.x);
//이동 제한하기(Lefrbound/Rightbound)
var clamp = Mathf.Clamp(this.transform.position.x, LeftBound, RightBound);
var pos=this.transform.position;
pos.x = clamp;
this.transform.position = pos;
//Debug.Log(clamp);
//Attack
if(Input.GetKeyDown(KeyCode.Space))
{
Debug.Log("Attack!");
this.state = eState.Attack;
this.anim.SetInteger("State", (int)this.state); //열거형식 사용가능
//Attack에서 Idle로 넘어가지 않는 오류. loop제거했는데도 문제가있다
Debug.Log(this.anim); //Attack이 끝나면 Idle로 넘어가야한다, 시간이 필요하다
}
if(this.state==eState.Attack)
{
//Attack이 끝났는지?
this.delta += Time.deltaTime;
if(this.delta>=0.367f)//Attack애니메이션 length 0.367
{
//Attack anim 종료
this.delta= 0;
this.state= eState.Idle; //State변경
}
}
}
}
#Idle-Attack으로 전환 성공
#Run-Attack도 가능하니 해봐야겠다
#다른 오브잭트와 상호작용(공격-체력-Die)까지 만들어보자