按住鼠标左键/space Microsoft VIsual Studio (Unity) 向上移动播放器

Moving Player Up by holding left mouse button /space Microsoft VIsual Studio (Unity)

我不太擅长Visual Studio。

我正在制作一个简单的游戏,当按下 SpaceLeft Mouse Button 时,我的 gameobject(玩家)应该向上移动。

这是我的代码

using UnityEngine;
using System.Collections;

public class PixelMovement : MonoBehaviour {

    Vector3 velocity = Vector3.zero;
    public Vector3 PressVelocity;
    public float maxSpeed = 5f;
    public float fowardSpeed = 1f;

    bool didPress = false;

    // Use this for initialization
    void Start () {

    }

    //Do Graphic & Input updates
    void update() {
        if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
            didPress = true;
        }
    }

    //Do physics engine updates here
    void FixedUpdate () {
        velocity.x = fowardSpeed;
        if (didPress == true){
            didPress = false;
            velocity += PressVelocity;
        }

        velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
        transform.position += velocity * Time.deltaTime;
    }
}

所以,它应该逆重力运动。当它停止持有时,它会继续下跌。我已经有了引力我只需要 "Up movement"

//Do Graphic & Input updates
void update() {
    if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
        didPress = true;
    }
}

我认为问题是因为 update() 应该是 Update()

尝试:

//Do Graphic & Input updates
void Update() {
    if(Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) {
        didPress = true;
    }
}

这是一个快速修复方法:您正在调用 Input.GetKeyDown() 和 Input.GetMouseButtonDown(),只有在按下按钮的第一帧 return 为真。

如果您想要重复事件(即鼠标按钮或 space 被 按住 按下),请使用 Input.GetKey(KeyCode.Space ) 和 Input.GetMouseButton(0).