【问题标题】:Moving Player Up by holding left mouse button /space Microsoft VIsual Studio (Unity)按住鼠标左键/空格键将播放器向上移动 Microsoft VIsual Studio (Unity)
【发布时间】:2016-05-09 18:32:42
【问题描述】:

我不太擅长 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;
    }
}

因此,它应该像与重力相反的方式移动。当它停止持有时,它会继续下跌。我已经有了重力,我只需要那个“向上运动”

【问题讨论】:

  • 您看到了什么行为?您是否已调试以查看实际调用了哪些代码,以及在您预期的程序流程的不同阶段存在哪些值?无需查看您的代码是否会执行它所说的,您的 update 方法命名错误。它应该是Update - 注意骆驼外壳。
  • 你试过提高前进速度吗?
  • 我确实需要像飞鸟这样的东西,但现在可以跳跃,但可以飞行(保持空间 - 向上移动)

标签: c# unity3d unityscript


【解决方案1】:
//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;
    }
}

【讨论】:

    【解决方案2】:

    这是一个快速解决方法:您正在调用 Input.GetKeyDown() 和 Input.GetMouseButtonDown(),它们仅在按下按钮的第一帧返回 true。

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

    【讨论】:

    • 我试过这个,但我的浮动有问题..当我保持空间时,它会减慢我的背景移动(背景移动速度较慢)我真的需要像飞扬的鸟这样的东西,但不是跳跃,而是飞行(保持空间向上移动)
    • 所以我解决了我的背景慢问题velocity += PressVelocity * Time.deltaTime; 但它仍然只是跳跃而不是飞行
    猜你喜欢
    • 2016-09-11
    • 1970-01-01
    • 1970-01-01
    • 2010-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-30
    相关资源
    最近更新 更多