【问题标题】:How to jump into a 2.5D world?如何跳入 2.5D 世界?
【发布时间】:2015-04-16 05:37:23
【问题描述】:

我是 Unity 的新手(以及一般的游戏开发)。我遵循了很棒的简单教程Survival Shooter,我有一个问题:在本教程中,我们为刚体的角色添加了一个 Y 约束位置,并将拖动值和角度拖动值设置为无限。由于这些设置会阻止角色移动到 Y 轴,我们如何让角色跳跃?

如果有人可以帮我解决这个问题,请...

非常感谢!

【问题讨论】:

    标签: unity3d physics 2.5d


    【解决方案1】:

    为什么要在 Y 轴上添加约束?您可以将其移除,然后添加重力,让您的玩家粘在地面上。之后,只需施加一个力,或者只是以设定的速度向上移动,让玩家跳跃,然后等待重力将他拉下来。

    【讨论】:

    • 我试图移除 Y 约束,但由于阻力和角度阻力值设置为无限,我无法施加任何向上的力。一种解决方案是为拖动和角度拖动设置一个值,并在按下“跳转”按钮时移除 Y 约束,然后在接地时重置这些值,但不确定是最佳解决方案...
    • 那么这里的问题是你为什么要添加所有这些约束?有什么特别的原因需要您的播放器上的这组严格的属性吗?
    【解决方案2】:

    这就是我要跳的。
    附:你需要移除向量上 Y 轴上的约束

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class Spawner : MonoBehaviour
    {
        public Vector3 force;
        public Rigidbody rb;
    
    
        // Use this for initialization
        void Start()
        {
        }
    
        // Update is called once per frame
        void Update()
        {
            if (Input.GetKey(KeyCode.Space) && transform.position.y == 0) //Enter your y axix where ground is located or try to learn a little more about raycasting ill just use 0 for an example)
            {
                rb.AddForce(force);//Makes you jump up when you hold the space button down line line 19 will do so that you only can jump when you are on the ground.  
    
            } if (Input.GetKeyUp(KeyCode.Space))
            {
                rb.AddForce(-force); //When you realase the force gets inverted and you come back to ground 
            }
        }
    
    }
    

    【讨论】:

      【解决方案3】:

      我会这样做,而不是编辑这篇文章的代码。

      using System.Collections;
      using System.Collections.Generic;
      using UnityEngine;
      
      public class Just : MonoBehaviour {
      
          public Vector3 force;
          public Rigidbody rb;
          bool isGrounded;
      
        
          // Update is called once per frame
          void Update()
          {
              if (Input.GetKeyDown(KeyCode.Space) && isGrounded == true) //Rember to got to your "Ground" object and tag it as Ground else this would not work 
              {
                  rb.AddForce(force);
              }       
          }
           void OnCollisionEnter(Collision collision)
          {
              if (collision.gameObject.tag == "Ground")
              {
                  isGrounded = true;
              }
          }
           void OnCollisionExit(Collision collision)
          {
              isGrounded = false;
          }
      
      }
      

      您需要为您的地面对象分配一个名为 Ground 的标签,您需要制作自己的名为 Ground 的标签称为地。并且还请记住在您的播放器对象上分配其他值。

      【讨论】:

        猜你喜欢
        • 2017-02-05
        • 2014-05-12
        • 2013-04-06
        • 1970-01-01
        • 1970-01-01
        • 2020-09-12
        • 1970-01-01
        • 1970-01-01
        • 2022-07-25
        相关资源
        最近更新 更多