코드 그대로 옮겨서 구현하면 된다고 하셧는데
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class PlayerController : MonoBehaviour
{
Rigidbody2D rigid2D;
Animator animator;
float jumpForce = 680.0f;
float walkForce = 15.0f;
float maxWalkSpeed = 1.5f;
float xmax = 2.70f;
float xmin = -2.70f;
//float threshold = 0.2f;
// Start is called before the first frame update
void Start()
{
this.rigid2D = GetComponent<Rigidbody2D>();
this.animator = GetComponent<Animator>();
}
public void LButtonDown()
{
int key = 0;
if (transform.position.x < -2.7) transform.position = new Vector3(xmin, transform.position.y, 1);
else key = -1;
float speedx = Mathf.Abs(this.rigid2D.velocity.x);
if (speedx < this.maxWalkSpeed)
{
this.rigid2D.AddForce(transform.right * key * this.walkForce);
}
if (key != 0)
{
transform.localScale = new Vector3(key, 1, 1);
}
if (this.rigid2D.velocity.y == 0)
{
this.animator.speed = speedx / 2.0f;
}
else
{
this.animator.speed = speedx / 2.0f;
}
if (transform.position.y < -10)
{
SceneManager.LoadScene("ClearScene");
}
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space)&&this.rigid2D.velocity.y==0) //점프
{
this.animator.SetTrigger("JumpTrigger");
this.rigid2D.AddForce(transform.up * this.jumpForce);
GetComponent<AudioSource>().Play();
}
int key = 0;
if (Input.GetKey(KeyCode.RightArrow))
if (transform.position.x > 2.7) transform.position = new Vector3(xmax, transform.position.y, 1);
else key = 1;
if (Input.GetKey(KeyCode.LeftArrow))
if (transform.position.x < -2.7) transform.position = new Vector3(xmin, transform.position.y, 1);
else key = -1;
float speedx = Mathf.Abs(this.rigid2D.velocity.x);
if (speedx < this.maxWalkSpeed)
{
this.rigid2D.AddForce(transform.right * key * this.walkForce);
}
if(key != 0)
{
transform.localScale = new Vector3(key, 1, 1);
}
if (this.rigid2D.velocity.y == 0)
{
this.animator.speed = speedx / 2.0f;
}
else
{
this.animator.speed = speedx / 2.0f;
}
if (transform.position.y < -10)
{
SceneManager.LoadScene("ClearScene");
}
}
private void OnTriggerEnter2D(Collider2D other)
{
Debug.Log("골");
SceneManager.LoadScene("ClearScene");
}
}
이렇게 구현했었는데 좌버튼을 눌러도 왼쪽만 보고 움직이지는 않더라구요 ㅠ
버튼은 누를때 한번만 함수가 호출되고 키는 누르는 동안 계속 함수가 호출되는데 그 차이 때문에 작동이 안 되는듯.
감사합니다. 구조 자체를 바꿔야하나보네용
아니면 팁같은게 있나요 이런상황에서? 계속 호출되게 하는?
버튼을 누르고 있을때 임의의 bool값을 true로 만들도록 하고 update에 있는 input.getkey 대신에 그 bool값을 넣으면 됨. 근데 버튼을 누르고 있음을 감지하는게 초보 입장에서는 어려울수도 있음
https://stackoverflow.com/questions/55448551/unity3d-how-to-detect-when-a-button-is-being-held-down-and-released
버튼 누르고 있는거 감지하는거는 이거 참고해봐