Unity【触控输入】—— && 使用【Unity Remote 5】实时测试【手游】{ }

我与影子孤独终老i 提交于 2020-10-31 04:18:42

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;
		}
	}
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!