【问题标题】:Unity UI button to manipulate game objects用于操作游戏对象的 Unity UI 按钮
【发布时间】:2016-08-18 16:00:44
【问题描述】:

我正在尝试创建一组操作 2D 游戏对象的按钮,以及人们发布的所有关于它的 InputManager,但我认为这不是我想要做的。我正在寻找可以执行以下操作的 c# 代码:(我目前拥有的代码发布在下面)

  1. 有一个允许我移动对象的 onclick 方法
  2. 有一个Update()FixedUpdate() 方法来检查按钮是否仍被按下。

我的代码现在只是移动对象一次,之后没有注意到其余的点击。

非常感谢收到的所有帮助,谢谢。

using UnityEngine;
using System.Collections;

public class fScript : MonoBehaviour {

    public GameObject player;
    private Rigidbody2D rb;
    Vector3 v;

    // Use this for initialization
    void Start () {
        rb = player.gameObject.GetComponent<Rigidbody2D> ();
        v = new Vector3 (10, 0, 0);
    }

    // Update is called once per frame
    public void MovePLayer () {
        rb.AddForce (v);
        Debug.Log ("CLICKED");
    }
}

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    在这种情况下,您需要尝试使用来设置速度。下一刻,速度不断减慢。如果您将每帧更改速度,则按下按钮时您将具有恒定的速度。 但还有一件事。您需要申请的不是 OnClick 事件!据我所知,您需要在 UI 按钮中未实现的 OnPress 事件。 尝试使用下一个代码解决此问题:

    private bool MouseIsDown;
    
    void OnPointerDown(){
          MouseIsDown = true;
    }
    void OnPointerUp(){
          MouseIsDown = false;
    }
    

    当 MouseIsDown== true 时,需要在 IF 主体中的固定更新内具有速度的代码;

    【讨论】:

    • 这不是我想要的。我刚刚在fixedUpdate 方法中使用屏幕触摸点,并根据用户触摸的位置施加力。不过,为了避免超加速,我限制了最大速度。不过,我确实很感谢您的回复!
    猜你喜欢
    • 1970-01-01
    • 2021-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-05
    • 1970-01-01
    相关资源
    最近更新 更多