如何将键盘转换为触控器

问题描述 投票:0回答:1

美好的一天,我是团结的新手#。有谁知道如何将我的2d自上而下汽车控制器脚本从键盘转换为触摸按钮。我需要一个触摸按钮,它有我的2d自上而下车的休息,后退,加速和左右按钮。

我的代码是基于街机风格的汽车游戏。

    float speedForce = 15f;
    float torqueForce = -200f;
    float driftFactorSticky = 0.9f;
    float driftFactorSlippy = 1;
    float maxStickyVelocity = 2.5f;
    float minSlippyVelocity = 1.5f; // <--- Exercise for the viewer

    // Use this for initialization
    void Start () 
    {

    }

    void Update() 
    {
        // check for button up/down here, then set a bool that you will use in FixedUpdate
    }

    // Update is called once per frame
    void FixedUpdate () 
    {
        Rigidbody2D rb = GetComponent<Rigidbody2D>();

        Debug.Log(RightVelocity().magnitude);

        float driftFactor = driftFactorSticky;
        if(RightVelocity().magnitude > maxStickyVelocity) {
            driftFactor = driftFactorSlippy;
        }

        rb.velocity = ForwardVelocity() + RightVelocity()*driftFactor;

        if(Input.GetButton("Accelerate")) {
            rb.AddForce( transform.up * speedForce );

            // Consider using rb.AddForceAtPosition to apply force twice, at the position
            // of the rear tires/tyres
        }
        if(Input.GetButton("Brakes")) {
            rb.AddForce( transform.up * -speedForce/2f );

            // Consider using rb.AddForceAtPosition to apply force twice, at the position
            // of the rear tires/tyres
        }

        // If you are using positional wheels in your physics, then you probably
        // instead of adding angular momentum or torque, you'll instead want
        // to add left/right Force at the position of the two front tire/types
        // proportional to your current forward speed (you are converting some
        // forward speed into sideway force)
        float tf = Mathf.Lerp(0, torqueForce, rb.velocity.magnitude / 2);
        rb.angularVelocity = Input.GetAxis("Horizontal") * tf;
    }

    Vector2 ForwardVelocity() 
    {
        return transform.up * Vector2.Dot( GetComponent<Rigidbody2D>().velocity, transform.up );
    }

    Vector2 RightVelocity() 
    {
        return transform.right * Vector2.Dot( GetComponent<Rigidbody2D>().velocity, transform.right );
    }
}
c# unity3d 2d
1个回答
0
投票

代替

if(Input.GetButton("Accelerate")) 
{
    rb.AddForce( transform.up * speedForce );
}
if(Input.GetButton("Brakes")) 
{
    rb.AddForce( transform.up * -speedForce/2f );
}

rb.angularVelocity = Input.GetAxis("Horizontal") * tf;

只需检查一些bool,并为它们设置相应的setter方法(你需要能够通过UnityEvents调用它们):

private bool acceleratePressed;
private bool brakePressed;
private bool leftPressed;
private bool rightPressed;

public void SetAccelerate(bool pressed)
{
    acceleratePressed = pressed;
}

public void SetBrake(bool pressed)
{
    brakePressed = pressed;
}

public void SetRightPressed(bool pressed)
{
    rightPressed = pressed;
}

public void SetLeftPressed(bool pressed)
{
    leftPressed = pressed;
}

private void FixedUpdate()
{
    if(acceleratePressed) 
    {
        rb.AddForce( transform.up * speedForce );
    }
    if(brakePressed)
    {
        rb.AddForce( transform.up * -speedForce/2f );
    }

    var leftRight = 0;
    if(leftPressed && !rightPressed)
    {
        leftRight = -1;
    }
    else if(!leftPressed && rightPressed)
    {
        leftRight = 1;
    }

    // If you are using positional wheels in your physics, then you probably
    // instead of adding angular momentum or torque, you'll instead want
    // to add left/right Force at the position of the two front tire/types
    // proportional to your current forward speed (you are converting some
    // forward speed into sideway force)
    float tf = Mathf.Lerp(0, torqueForce, rb.velocity.magnitude / 2);
    rb.angularVelocity = leftRight * tf;
}

不幸的是,Unity UI.Button无法向下或向上转发按钮..只有onClick

但您可以自己轻松实现其他活动!你可以使用IPointerDownHandlerIPointerUpHandlerIPointerExitHandler接口和自定义UnityEvents

我不喜欢继承UI.Button(有些人可能更喜欢),而是在单独的组件中添加其他行为。

还有其他方法如何完全实现它,但我喜欢保持可重用的东西,所以我会像它一样使用它

[RequireComponent((typeof(Button)))]
public class DownUpButtonEnhancement : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
    private Button _button;

    public UnityEvent OnButtonDown;
    public UnityEvent OnButtonUp;

    private void Awake()
    {
        _button = GetComponent<Button>();
    }

    //Detect current clicks on the GameObject (the one with the script attached)
    public void OnPointerDown(PointerEventData pointerEventData)
    {
        if (!_button.interactable) return;

        OnButtonDown?.Invoke();
    }

    //Detect if clicks are no longer registering
    public void OnPointerUp(PointerEventData pointerEventData)
    {
        OnButtonUp?.Invoke();
    }

    // Detect if pointer leaves the object
    public void OnPointerExit(PointerEventData eventData)
    {
        // additionally reset if pointer leaves button while pressed
        OnButtonUp?.Invoke();
    }
}

把它放在你的两个用户界面Buttons上,配置相应的OnButtonDownOnButtonUp回调,就像你使用onClickButton一样:

  1. 使用您的移动脚本在GameObject中拖动
  2. 选择按下按钮的每一帧应调用的相应方法。
  3. 对于OnButtonDown传球true(选中方框)OnButtonUp传球为假。

enter image description here

然后在游戏中你可以看到现在按下按钮时启用了MoveController脚本的相应bool,如果pinter在按下时离开按钮,则另外重置

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.