2016-03-07 49 views
-3

所以我的代碼似乎工作得很好。除了跳躍之外,從動畫到重力到移動作品的所有內容。我看不出有什麼錯在我的代碼,將不允許跳工作,下面的代碼:我的角色不會跳。 (Unity2D C#)

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

即使移動= 0,我的角色仍然可以翻轉。爲什麼翻轉和跳躍是同一件事?我在代碼中搞了些什麼? – AlanOmari

+0

通過它的外觀,你只能從if語句中調用'Flip()',即'move> 0 ||移動<0'因此你的角色不會在'move = 0'的情況下'翻轉' – TheLethalCoder

回答

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); 
     } 
    } 
+1

哦,我的上帝我不敢相信我真的錯過了。謝啦。 – AlanOmari