
* 겪고 있는 문제 상황을 최대한 자세하게 작성해주세요.
* 문제 해결을 위해 어떤 시도를 해보았는지 구체적으로 함께 알려주세요.
르탄이를 클릭했을 때 좌우로 이동하도록 하는 기능에 대해 질문드립니다.
보시면
if (transform.position.x >= 2.6f)
{
direction *= -1;
renderer.flipX = true;
}
if (transform.position.x <= -2.6f)
{
direction *= -1;
renderer.flipX = false;
}
direction이 둘다 -1로 되어 있는데
(자세한건 작성 및 코드에서 확인)
제가 인강에서 배운 것이랑 어떤 차이가 있을까요?
인강에서 배운 것은 이렇게 나오는데..
if(transform.position.x > 2.6f)
{
renderer.flipX = true;
direction = -0.05f;
}
if(transform.position.x < -2.6f)
{
renderer.flipX = false;
direction = 0.05f;
}
둘 다 똑같이 적용되는 것은 알겠는데 원리를 잘 모르겠습니다..
디렉션에 음수가 둘 다 들어가고
다른 스크립트는 음수, 양수가 들어가고...
작성한 코드 및 에러 메세지
float direction = 0.05f;
SpriteRenderer renderer;
// Start is called before the first frame update
void Start()
{
Application.targetFrameRate = 60;
renderer= GetComponent<SpriteRenderer>();
}
// [포스팅 된 것]
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
direction *= -1;
renderer.flipX = !renderer.flipX;
}
if (transform.position.x >= 2.6f)
{
direction *= -1;
renderer.flipX = true;
}
if (transform.position.x <= -2.6f)
{
direction *= -1;
renderer.flipX = false;
}
transform.position += Vector3.right * direction;
}
--------------------------------------------------------
// [인강에서 배운 것]
// Update is called once per frame
void Update()
{
if(Input.GetMouseButtonDown(0))
{
direction *= -1;
renderer.flipX = !renderer.flipX;
}
if(transform.position.x > 2.6f)
{
renderer.flipX = true;
direction = -0.05f;
}
if(transform.position.x < -2.6f)
{
renderer.flipX = false;
direction = 0.05f;
}
transform.position += Vector3.right * direction;
}
