using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class playercontroller : MonoBehaviour
{
Rigidbody2D rigid;
public float maxspeed;
public float Jumppower;
public int atk;
int jumpcnt = 0;
private float curtime;
public float cooltime = 0.5f;
public Transform pos;
public Transform pos2;
public Vector2 playerdir;
public Vector2 boxsize;
// Start is called before the first frame update
void Start()
{
rigid = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
float hinput;
float vinput;
hinput = Input.GetAxisRaw("Horizontal");
vinput = Input.GetAxisRaw("Vertical");
if (curtime <= 0)
{
//공격
if (Input.GetKey(KeyCode.Z))
{
if (hinput == 1)
{
Collider2D[] collider2ds = Physics2D.OverlapBoxAll(pos.position, boxsize, 0);
foreach (Collider2D collider in collider2ds)
{
Debug.Log(collider.tag);
if (collider.tag == "enemy")
{
collider.GetComponent<enmie>().takedamage(1);
}
}
}
else if (hinput == -1)
{
Collider2D[] collider2ds = Physics2D.OverlapBoxAll(pos2.position, boxsize, 0);
foreach (Collider2D collider in collider2ds)
{
Debug.Log(collider.tag);
if (collider.tag == "enemy")
{
collider.GetComponent<enmie>().takedamage(1);
}
}
}
curtime = cooltime;
}
}
else
{
curtime -= Time.deltaTime;
}
//점프
if (Input.GetButtonDown("Jump") && jumpcnt < 2)
{
rigid.AddForce(Vector2.up * Jumppower, ForceMode2D.Impulse);
jumpcnt++;
}
if (Input.GetButtonUp("Horizontal"))
{
//버튼을 때면 멈춤
rigid.velocity = new Vector2(rigid.velocity.normalized.x * 0.5f, rigid.velocity.y);
}
}
void FixedUpdate()
{
float h = Input.GetAxisRaw("Horizontal");
// 캐릭터 움직임
rigid.AddForce(Vector2.right * h, ForceMode2D.Impulse);
if(rigid.velocity.x > maxspeed)
{
rigid.velocity = new Vector2(maxspeed, rigid.velocity.y);
}
else if (rigid.velocity.x < maxspeed*(-1))
{
rigid.velocity = new Vector2(maxspeed*(-1), rigid.velocity.y);
}
//땅
Debug.DrawRay(rigid.position, Vector3.down, new Color(0, 1, 1));
RaycastHit2D Rayhit = Physics2D.Raycast(rigid.position, Vector3.down, 1, LayerMask.GetMask("platfrom", "enemy"));
// 땅에 닿는 것들중 enemy, platfrom 이면 값 전환
if (rigid.velocity.y < 0) // 떨어질 때
{
if (Rayhit.collider != null)
{
Debug.Log(Rayhit.collider.tag);
jumpcnt = 0;
if (Rayhit.distance < 0.5f)
{
Debug.Log(Rayhit.collider.name);// 땅에 발이 닿으면
}
}
}
}
private void OnDrawGizmos()
{
Gizmos.color = Color.blue;
Gizmos.DrawWireCube(pos.position, boxsize);
Gizmos.DrawWireCube(pos2.position, boxsize);
}
}
유튜브나 책 보면서 조금씩 해보고 있는데 도저히 모르겠어서 질문해봄
공격에 대한 질문인데 코드를 보면 알 수 있듯
pos, pos2 에 enemy가 들어오고 z키를 누르면 로그가 떠야하는데
조건문을 걸어서 그런가 방향키를 누르고 z키를 눌러야 공격로그가 뜸
가만히 있으면 hinput에 0이 반환되서 로그가 안뜸
이거 해결방법좀 알수 있을까..?
이제 막 시작해서 코드가 조금 더러울수도....
해당 댓글은 삭제되었습니다.
알겟음...! 고마워!