mlmeier Posted June 23, 2014 Share Posted June 23, 2014 Moin Leute, da ich zurzeit ein freies Schulprojekt habe, habe ich mich dort für das Programmieren eines 2D Platformer entschieden. Ich bin ein blutiger Anfänger, weshalb ich einen Fehler nicht finde. Der Character Springt nicht hoch. Ich habe auch schon die Gravity runter gestellt, daran liegt es aber nicht. Hier ist der Code: Player Controller: using UnityEngine;using System.Collections;public class PlayerController : MonoBehaviour { public float gravity = 1; public float speed = 10; public float jumpPower = 10; bool inputJump = false; float velocity = 0; Vector3 moveDirection = Vector3.zero; CharacterController characterController; SpriteController spriteController; bool lookRight = true; // Use this for initialization void Start () { characterController = GetComponent<CharacterController>(); spriteController = GetComponent<SpriteController>(); } // Update is called once per frame void Update () { InputCheck(); Move (); SetAnimation (); } void InputCheck() { velocity = Input.GetAxis ("Horizontal") * speed; if (velocity > 0) lookRight = true; if (velocity < 0) lookRight = false; if (Input.GetKeyDown (KeyCode.Space)) { inputJump = true; } else { inputJump = false; } } void Move() { if (characterController.isGrounded) { if (inputJump) moveDirection.y = jumpPower; } moveDirection.x = velocity; moveDirection.y -= gravity; characterController.Move (moveDirection * Time.deltaTime); } void SetAnimation() { if (velocity > 0) { spriteController.SetAnimation(SpriteController.AnimationType.goRight); } if (velocity < 0) { spriteController.SetAnimation(SpriteController.AnimationType.goLeft); } if (velocity == 0) { if (lookRight) { spriteController.SetAnimation (SpriteController.AnimationType.stayRight); } else { spriteController.SetAnimation (SpriteController.AnimationType.stayLeft); } } if (!characterController.isGrounded) { if (lookRight) { spriteController.SetAnimation (SpriteController.AnimationType.jumpRight); } else { spriteController.SetAnimation (SpriteController.AnimationType.jumpLeft); } } }} Sprite Controller: using UnityEngine;using System.Collections;using System.Collections.Generic;public class SpriteController : MonoBehaviour { public List<Texture2D> animationStayRight; public List<Texture2D> animationStayLeft; public List<Texture2D> animationGoRight; public List<Texture2D> animationGoLeft; public List<Texture2D> animationJumpRight; public List<Texture2D> animationJumpLeft; public List<Texture2D> animationAttackRight; public List<Texture2D> animationAttackLeft; public float speed = 1; public AnimationType currentAnimationType = AnimationType.stayRight; public enum AnimationType{ stayRight, stayLeft, goRight, goLeft, jumpRight, jumpLeft, attackRight, attackLeft } // Use this for initialization void Start () { } // Update is called once per frame void Update () { switch (currentAnimationType) { case AnimationType.stayRight: SetTexture (animationStayRight); break; case AnimationType.stayLeft: SetTexture (animationStayLeft); break; case AnimationType.goRight: SetTexture (animationGoRight); break; case AnimationType.goLeft: SetTexture (animationGoLeft); break; case AnimationType.jumpRight: SetTexture (animationJumpRight); break; case AnimationType.jumpLeft: SetTexture (animationJumpLeft); break; case AnimationType.attackRight: SetTexture (animationAttackRight); break; case AnimationType.attackLeft: SetTexture (animationAttackLeft); break; } } void SetTexture (List<Texture2D> animationSprite) { int index = (int) (Time.time * speed); index = index % animationSprite.Count; renderer.material.mainTexture = animationSprite[index]; } public void SetAnimation(AnimationType animationType) { currentAnimationType = animationType; }} Ich hoffe jemand findet den Fehler, also danke schon mal im vorraus. LG Moritz Link to comment Share on other sites More sharing options...
Headshooter Posted August 9, 2014 Share Posted August 9, 2014 (edited) Das liegt schon sehr weit weg.Ich empfehle dir auf eine Programmierseite soetwas zu melden z.B. byte-welt.net oder die Seite von Microsoft.Da gibt es viele Leute die sich mit C auskennen, ich kenne mich nur mit C++(CLI) aus. EDIT:Ich empfehle dir Vector3 moveDirection = Vector3.zero; sowas zu begründen, was ist Vector3 der Spieler? Edited August 9, 2014 by Headshooter Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now