【问题标题】:UNITY 2D: Player flying when space/mousebutton is holdedUNITY 2D:按住空格键/鼠标键时玩家飞行
【发布时间】:2016-09-11 11:32:49
【问题描述】:

所以当space 或mousebutton 被按下时,我需要我的角色上升。我将 rigbody 2d 和 box colider 2d 添加到我的gameobject(玩家)。问题是当我点击space 或mousebutton时,它只会跳跃而不是不断向上移动。

这是我的代码:

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;
    }
}

【问题讨论】:

  • 我已经尝试了您的代码并且在我的计算机上运行良好。 Here I left a link with the video。你有没有附加到这个游戏对象的其他脚本?
  • 这是2d 不是3d
  • 我附加了box colider 2d 并取消选中is triggered,我还添加了rigbody2d
  • 您是否将 RigitBody2D 质量设置为零?
  • 不,我使用自动质量,我解决了这个问题,但现在我遇到了重力问题。如果我使用重力刻度为 1,我的物体会立即粗暴地坠落,不像圆形......

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


【解决方案1】:

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

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;
    }
}

【讨论】:

    【解决方案2】:

    如果您使用重力和 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);
            }
        }
    }
    

    【讨论】:

      【解决方案3】:
          if (Input.GetKey(KeyCode.Space) || Input.GetMouseButton(0))
          {
              didPress = true;
          }
          else
          {
              didPress = false;
          }
      

      并从 FixedUpdate() 函数中删除 didPress = false;

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

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

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

      【讨论】:

        【解决方案4】:

        FixedUpdate 可以在每帧连续调用多次。这基于项目的物理设置。您可以对其进行编辑以增加或减少物理的准确性。

        每帧只调用一次更新。

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

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

        【讨论】:

          猜你喜欢
          • 2021-03-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-12-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多