UNITY 2D:当空格/鼠标按钮被占用时,玩家飞行

时间:2016-05-15 16:48:28

标签: c# visual-studio unity3d unityscript unity3d-2dtools

因此,当按下spacemousebutton时,我需要让我的角色上升。我在我的gameobject(玩家)中添加了rigbody 2d和box colider 2d。问题是,当我点击spacemousebutton时,它只会跳跃而不是向上移动。

这是我的代码:

using UnityEngine;
using System.Collections;

public class PixelMovement : MonoBehaviour {

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

    bool didPress = false;

    // Use this for initialization
    void Start () {

    }

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

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

        }

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

4 个答案:

答案 0 :(得分:0)

    if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0))
    {
        didPress = true;
    }
    else
    {
        didPress = false;
    }

并从didPress = false;功能

中删除FixedUpdate()

编辑:好的,等等,我刚刚注意到你在FixedUpdate()更新了transform.position,这不是一个好主意。当你这样做时,你基本上将你的对象从一个位置传送到另一个位置而忽略了所有类型的物理(这可能是你使用FixedUpdate()的原因)。您的脚本与其他对象冲突会给您带来奇怪的结果。查找有关使用RigidBodies进行适当的物理感知移动的教程,或者只是将所有代码移动到Update(),因为此时您可能有一些框架,您的对象不会移动,而其他框架会移动两次距离。这将发生,因为FixedUpdate()可能会在一个帧中执行任意次数(包括0),具体取决于设置。 FixedUpdate默认情况下每秒运行50次,而默认情况下,Update每秒运行大约60次(在移动设备30上)。最后,在FixedUpdate中永远不要使用Time.deltaTime,请使用Time.fixedDeltaTime

Time.deltaTime大约等于1/60秒(1帧持续时间)

Time.fixedDeltaTime大约是1/50秒(1个物理更新持续时间)

答案 1 :(得分:0)

如果你正在使用重力和RigitBody2D的质量。我建议你使用RigitBody2D.Addforce()方法来跳跃你的角色,因为如果你只是改变了角色位置,那么引力会立刻将他转向地面

此代码创建“火箭手”的效果,当用户按下空格键时它会起飞:

using UnityEngine;
using System.Collections;

public class jumpAntigravity : MonoBehaviour {

    Vector2 upForce;

    void Start () {
        //Up force set to double of gravity
        upForce = new Vector2(0, 9.8f*2);
    }

    void Update () {
        if (Input.GetKey(KeyCode.Space))
        {
            print("Spacebar is down");
            GetComponent<Rigidbody2D>().AddForce(upForce);
        }
    }
}

答案 2 :(得分:0)

好的,这对我有用。谢谢大家的帮助:)

using UnityEngine;
using System.Collections;

public class PixelMovement : MonoBehaviour {

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

    bool didPress = false;

    void Update()
    {
        if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0))
        {
            didPress = true;
        }
        else
        {
            didPress = false;
        }
    }

    void FixedUpdate()
    {
        velocity.x = fowardSpeed;
        transform.position += velocity * Time.deltaTime;
        if (didPress == true)
        {

            float amntToMove1 = playerSpeed * Time.deltaTime;
            transform.Translate(Vector3.up * amntToMove1);
        }
        velocity = Vector3.ClampMagnitude(velocity, maxSpeed);
        transform.position += velocity * Time.deltaTime;
    }
}

答案 3 :(得分:0)

FixedUpdate可以连续多次调用。这基于项目的物理设置。您可以对其进行编辑,以便为物理学添加更多或更少的精确度。

每帧只调用一次更新。

所以你想避免在FixedUpdate中做逻辑。如果要移动刚体,请使用MovePosition。

来自Unity文档:如果在Rigidbody上启用了Rigidbody插值,则调用Rigidbody.MovePosition会导致渲染的任何中间帧中两个位置之间的平滑过渡。如果要在每个FixedUpdate中连续移动刚体,则应使用此方法。

相关问题