如何使“玩家”在按住鍵時移動?我在 GUI 中有一個按鈕
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moveLeft : MonoBehaviour
{
public Transform player;
public void Start(){
player.transform.position = new Vector3
(player.position.x-1, player.position.y, player.position.z);
}
}
uj5u.com熱心網友回復:
Input.GetKey(KeyCode.LeftArrow) 表示您在鍵盤中按住左箭頭鍵。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class moveLeft : MonoBehaviour
{
public Transform player;
[SerializableField] float speed;
void Update(){
if(Input.GetKey(KeyCode.LeftArrow))
player.transform.position = Vector3.left * speed * Time.deltaTime;
}
}
編輯:
using UnityEngine;
using UnityEngine.EventSystems;
public class moveLeft : MonoBehaviour, IPointerDownHandler, IPointerUpHandler
{
public Transform player;
[SerializableField] float speed;
void Update(){
if(isActive)
player.transform.position = Vector3.left * speed * Time.deltaTime;
}
bool isActive = false;
public void OnPointerDown(PointerEventData eventData)
{
isActive = true;
}
public void OnPointerUp(PointerEventData eventData)
{
isActive = false;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/435340.html
上一篇:在javascript中使用filter()來實作curriable()是可行的,但是,uisngmap()是可行的,為什么?
下一篇:unity,獲取UI按鈕的名稱
