C# - 2D-platformer-Bewegung code

Dieser code wird immer springen, auch wenn es nicht auf dem Boden wie kann man aufhören (mit Einheit).

Code:

using UnityEngine;
using System.Collections;

public class PlayerController : MonoBehaviour 
{

    //Movement
    public float speed;
    public float jump;
    float moveVelocity;

    //Grounded Vars
    bool grounded = true;

    void Update () 
    {
        //Jumping
        if (Input.GetKeyDown (KeyCode.Space) || Input.GetKeyDown (KeyCode.UpArrow) || Input.GetKeyDown (KeyCode.Z) || Input.GetKeyDown (KeyCode.W)) 
        {
            if(grounded)
            {
                GetComponent<Rigidbody2D> ().velocity = new Vector2 (GetComponent<Rigidbody2D> ().velocity.x, jump);
            }
        }

        moveVelocity = 0;

        //Left Right Movement
        if (Input.GetKey (KeyCode.LeftArrow) || Input.GetKey (KeyCode.A)) 
        {
            moveVelocity = -speed;
        }
        if (Input.GetKey (KeyCode.RightArrow) || Input.GetKey (KeyCode.D)) 
        {
            moveVelocity = speed;
        }

        GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveVelocity, GetComponent<Rigidbody2D> ().velocity.y);

    }
    //Check if Grounded
    void OnTriggerEnter2D()
    {
        grounded = true;
    }
    void OnTriggerExit2D()
    {
        grounded = false;
    }
}
  • sieht aus wie Sie nicht gebunden, die Auslöser für alles?
  • Wenn Sie verbessern diese Frage ein bisschen, es kann sein, on-topic über das Spiel Entwicklung stackexchange Seite, vorausgesetzt, Sie sind nicht in Frage-es blockiert schon.
  • BTW die Einheit Frage-site ist ziemlich gut, um schnell Antworten zu
InformationsquelleAutor | 2015-10-02
Schreibe einen Kommentar