简体   繁体   English

我的c#播放器跳码怎么回事? (统一 3d)

[英]What is wrong with my c# player jumping code ? (unity 3d)

I just started learning unity 3d, and I wrote c# code that makes my player jump but it is not working我刚开始学习统一 3d,我写了 c# 代码让我的播放器跳起来但它不起作用

public Rigidbody rb;
public float SideWaysForce = 500f;
bool CanJump;

    void OnCollisionEnter(Collision collisioninfo)
    {
        if (collisioninfo.collider.tag == "Ground")
        {
            Debug.Log("Can Jump");
            CanJump = true;
        }
        
        if (collisioninfo.collider.tag != "Ground")
        {
            Debug.Log("Can not Jump");
            CanJump = false;
        }
    }

void FixedUpdate()

if (CanJump == true && Input.GetKey("space"))
        {
             rb.AddForce(0, SideWaysForce * Time.deltaTime, 0);
        }

no error messages were shown in the Console控制台中未显示任何错误消息

Well, debug (with a debugger or with Debug.Log ) if the script even works, then make sure both CanJump and Input.GetKey("Space") are true.好吧,调试(使用调试器或使用Debug.Log )如果脚本甚至可以工作,那么请确保CanJumpInput.GetKey("Space")都为真。 Also, you probably should use Input.GetKeyDown if you don't want your character to fly when you're holding a key.此外,如果您不想让角色在握着钥匙时飞起来,您可能应该使用Input.GetKeyDown

  1. Make sure the player has a rigid body attached确保玩家连接了刚体
  2. Make sure the player has a Collider attached (eg. mesh collider)确保玩家连接了对撞机(例如网格对撞机)
  3. Make sure the player has that script attached.确保播放器附加了该脚本。
  4. Make sure the RigidBody (rb) is assigned to the player's rigid body in inspector.确保 RigidBody (rb) 在检查器中分配给玩家的刚体。
  5. Make sure sideways force is the same value in the inspector and in the code (it should be 500f on both).确保检查器和代码中的侧向力值相同(两者都应为 500f)。
  6. In Fixed Update you should have Input.GetKey("Space") not Input.GetKey("space") (I believe the capital letter matters).在固定更新中,您应该使用Input.GetKey("Space")而不是Input.GetKey("space") (我相信大写字母很重要)。
  7. Make sure your ground object has the "Ground" tag attached.确保您的接地 object 附有“接地”标签。

If non of these solutions fix your issue please tell me.如果这些解决方案都不能解决您的问题,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM