1.確認再canvas組件下方
2.要再canvas的組件范圍內(nèi),按照相對距離去擺放button的位置
3.綁定腳本
4.通過GameObject的GetComponent函數(shù)去調(diào)用人物組件腳本的移動函數(shù)
[SerializeField] private GameObject player_object;
PlayerMovement player = player_object.GetComponent<PlayerMovement>();
player.LeftMove();
5.通過代碼實現(xiàn)button點擊和彈起響應(yīng)時間
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中