我的角色不会跳跃。 (Unity2D C#)

时间:2016-03-07 16:28:21

标签: c# unity3d

所以我的代码似乎工作正常。除了跳跃之外,从动画到重力到移动的所有东西都是有效的。我无法在代码中看到哪些内容不允许跳转工作,这就是代码:

using UnityEngine;
using System.Collections;
using System;

public class CharacterRun : MonoBehaviour
{

   public float MaxSpeed = 10;
   bool FacingRight = true;
   Animator anim;
   bool grounded = false;
   public Transform groundCheck;
   float groundRadius = 0.2f;
   public LayerMask whatIsGround;
   public float jumpForce = 700f;

   // Use this for initialization
   void Start()
   {
      anim = GetComponent<Animator>();
   }

   // Update is called once per frame
   void FixedUpdate()
   {
       grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
       anim.SetBool("Ground", grounded);
       anim.SetFloat("vSpeed", GetComponent<Rigidbody2D>().velocity.y);

       float move = Input.GetAxis("Horizontal");

       anim.SetFloat("hSpeed", Mathf.Abs(move));

       GetComponent<Rigidbody2D>().velocity = new Vector2(move * MaxSpeed, GetComponent<Rigidbody2D>().velocity.y);
       GetComponent<Rigidbody2D>().constraints = RigidbodyConstraints2D.FreezeRotation;

       if (move > 0 && !FacingRight)
           Flip();
       else if (move < 0 && FacingRight)
           Flip();
   }

   private void SetFloat(string v1, float v2)
   {

   }
   void update ()
   {
       if (grounded && Input.GetKeyDown(KeyCode.Space))
       {
           anim.SetBool("Ground", false);
           GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
       }

   }
   void Flip()
   {
       FacingRight = !FacingRight;
       Vector3 theScale = transform.localScale;
       theScale.x *= -1;
       transform.localScale = theScale;
   }
}

1 个答案:

答案 0 :(得分:6)

因为你需要命名函数Update而不是更新。

 void Update ()  /// Not update
 {
       if (grounded && Input.GetKeyDown(KeyCode.Space))
       {
           anim.SetBool("Ground", false);
           GetComponent<Rigidbody2D>().AddForce(new Vector2(0, jumpForce), ForceMode2D.Impulse);
       }
  }