using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class KeyPressChangeAni : MonoBehaviour
{
public string player = "";
public string left = "";
public string right = "";
string nowMode = "";
public float speed = 2;
public float jumppower = 4;
float vx = 0;
bool pushFlag = false;
bool jumpFlag = false;
bool groundFalg = false;
Rigidbody2D rbody;
void Start()
{
rbody = GetComponent<Rigidbody2D>();
rbody.constraints = RigidbodyConstraints2D.FreezeRotation;
nowMode = player;
}
// Update is called once per frame
void Update()
{
if (Input.GetKey("left"))
{
nowMode = left;
vx = -speed;
}
if (Input.GetKey("right"))
{
nowMode = right;
vx = speed;
}
if (Input.GetKeyUp("left"))
{
nowMode = player;
vx = 0;
}
if (Input.GetKeyUp("right"))
{
nowMode = player;
vx = 0;
}
if (Input.GetKey("space")&&groundFalg)
{
if(pushFlag==false)
{
jumpFlag = true;
pushFlag = true;
}
}else
{
pushFlag = false;
}
}
void FixedUpdate()
{
rbody.velocity = new Vector2(vx, rbody.velocity.y);
this.GetComponent<Animator>().Play(nowMode);
this.transform.Translate(vx / 50, 0, 0);
if(jumpFlag)
{
jumpFlag = false;
rbody.AddForce(new Vector2(0, jumppower), ForceMode2D.Impulse);
}
}
void OnTriggerStay2D(Collider2D collision)
{
groundFalg = true;
}
void OnTriggerExit2D(Collider2D collision)
{
groundFalg = false;
}
}
if 문이 4번 연속으로 거쳤네...
내가 짜던 방식하고 비슷함...
저 의미를 알아야 할텐데
left 제어문 다음에 right 제어분 또 검사해서 Update 계속 돌고...