Unity【觸控輸入】—— && 使用【Unity Remote 5】實時測試【手遊】{ }

2020-10-02 13:00:27

1.控制人物行走

private void Walk()
{
#if UNITY_EDITOR
	moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), rb2D.velocity.y);
	rb2D.velocity = moveInput * moveSpeed * Time.deltaTime;
#else
	if (Input.touchCount > 0)
	{
		Touch touch = Input.GetTouch(0);
		touchPos = Camera.main.ScreenToWorldPoint(touch.position);
		touchPos.z = 0f;
		if (touchPos.x > 0 && Vector2.Distance(transform.position, touchPos) > 0.5f)
		{
			moveInput.x = 1f;
		}
		else if(touchPos.x < 0 && Vector2.Distance(transform.position, touchPos) > 0.5f)
		{
			moveInput.x = -1f;
		}
	}
	else
	{
		moveInput.x = 0f;
	}
#endif

	playerAnim.SetBool("walk", moveInput.x != 0 ? true : false);

	if (moveInput.x != 0)
	{
		if (moveInput.x < 0)
		{
			sr.flipX = true;
		}
		else
		{
			sr.flipX = false;
		}
	}
}