1.确认再canvas组件下方
2.要再canvas的组件范围内,按照相对距离去摆放button的位置
3.绑定脚本
4.通过GameObject的GetComponent函数去调用人物组件脚本的移动函数
[SerializeField] private GameObject player_object;
PlayerMovement player = player_object.GetComponent<PlayerMovement>();
player.LeftMove();
5.通过代码实现button点击和弹起响应时间
using UnityEngine.EventSystems;
public class MoveButtons : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
public void OnPointerDown(PointerEventData eventData)
{
Debug.Log("Button Pressed");
is_left_press = true;
}
public void OnPointerUp(PointerEventData eventData)
{
Debug.Log("Button Released");
is_left_press = false;
}
6.注意rb.velocity = new Vector2(dirX * moveSpeed, rb.velocity.y);要放在update中