SimpleScript Geschrieben 27. November 2015 Melden Share Geschrieben 27. November 2015 Hallo werte Community, Ich wende mich heute mal wieder an euch, da sich mir wiedermal ein Problem auftut, welches ich einfach nicht gelöst kriege. Mein Problem: Ich arbeite jetzt seit einigen Tagen an einen TopDown Shooter, mithilfe eines Assets welches ich vor einiger Zeit im Unitystore erworben habe. Ich habe schon einige Teile umgeschrieben um den ganzen Spaß etwas an mein Ziel anzupassen. Nun habe ich mich heute daran gemacht mich in das "neue" Animationsystem für die Sprites einzuarbeiten, um meine Spielfigur etwas leben einzuhauchen. Genutzt habe ich dafür diese Anleitung: http://johnstejskal....ets-in-unity3d/ Nachdem ich meine "erste" Waffe mit den passenden Animationen eingebaut habe, was ja auch soweit geklappt hat, wollte ich die Spielfigur um eine weitere Waffe ergänzen. Hier tut sich mir aber schon seit 4 Stunden das Problem auf, dass der Waffenwechsel partu nicht klappen will, wodurch ich mich an euch wende. Was ich erreichen will/wollte: Die Spielfigur soll die Waffen wechseln, bewegen und schießen können. Der Waffenwechsel vom Code her gesehen klappt soweit, nur halt der Animationswechsel nicht. Meine Vermutung: Über den Code hinweg wird scheinbar immer wieder ein Wert auf den Ursprungszustand gesetzt, sodass der Wechsel nicht funktioniert. Mein Code sieht derzeit noch ziemlich schlimm aus(Umlaute, verschiedene Bezeichnungen für gleiche Sache, unvorteilhafte Variablen, tot Varialben, keine Kommentare, usw, ich bin mir diesen Fehlern bewusst und werde sich auch noch angehen) da ich schon einiges ausprobiert habe, um dem Problem her zu werden. Vielleicht habt ihr ja einen Lösungs-/Verbesserungsvorschlag. Der Code: using UnityEngine; using System.Collections; public class Player : MonoBehaviour { Animator animator; bool _isPlaying_walk = false; bool _isPlaying_shoot = false; bool _isPlaying_pistole = false; const int WEAPON_RILFE = 0; const int WEAPON_PISTOLE = 2; const int STATE_IDLE = 0; const int STATE_WALK = 1; const int STATE_SHOOT = 2; int _currentAnimationState = STATE_IDLE; int _currentAnimationWeapon = WEAPON_PISTOLE; public bool walk = false; SpriteRenderer sr; public static float moveSpeed; //The speed at which the player will move at private Rigidbody2D rig; //The player's rigidbody2D component public LayerMask mask; //LayerMask defining which layer can be affected by the player's attacks public Camera cam; //The camera in the scene public static int money; //The player's money public static int curHp; //The player's current health public static int maxHp; //The player's maximum health //Weapon public static int ammo; //The player's current ammunition public static int assaultrilfeammo; public static int assaultrilfemagazin; public static int shotgunammo; public static int startAmmo; //The ammunition which the player will start each round with public static int shootDist; //The distance of how far the player's bullets will travel public int damage; //The damage that the player will deal public double firerate = 0.50; public double lastshot = 0.00; public GameObject muzzleFlash; //The muzzle flash effect that will be instantiated once the gun is shot public GameObject muzzle; //The point of origin where the end of the player's gun muzzle is public GameObject muzzleFlash_ar; //The muzzle flash effect that will be instantiated once the gun is shot public GameObject muzzle_ar; public GameObject muzzleFlash_sg; public GameObject muzzle_sg; public GameObject laser; //The laser public static bool hasSight; //Does the player have the Sight upgrade public static bool hasExtendedMag; //Does the player have the Extended Mag upgrade public static bool hasLaser = true; //Does the player have the Laser upgrade float reloadtime = 5.0f; public float timer; public static bool unarmed = false; public static bool pistole = false; public static bool pistoleausgerüstet = false; public static bool sturmgewehr = false; public static bool sturmgewehrausgerüstet = false; public static bool shotgun = false; public static bool shotgunausgerüstet = false; //Audio public double walkrate = 0.50; public double lastwalk = 0; public AudioSource asource; //The players AudioSource that sounds will be played through public AudioClip shootSound; //The audio clip that will be played when the player fires a bullet public AudioClip shootgunSound; public AudioClip dryFireSound; //The audio clip that will be played when the player has no more ammo but continues to click public AudioClip walksound; void Start() { animator = this.GetComponent<Animator>(); rig = transform.GetComponent<Rigidbody2D>(); startAmmo = 100; ammo = startAmmo; moveSpeed = 100.0f; shootDist = 100; assaultrilfeammo = 30; assaultrilfemagazin = 3; shotgunammo = 50; curHp = 100; maxHp = 100; hasSight = false; hasExtendedMag = false; hasLaser = false; money = 5; laser.GetComponent<LineRenderer>().sortingLayerName = "Player"; asource.volume = PlayerPrefs.GetFloat("Volume"); } void Update() { if (pistoleausgerüstet == true) { changeWeapon(WEAPON_PISTOLE); } Move(); CameraFollow(); if (unarmed == true) { pistole = false; sturmgewehr = false; shotgun = false; } if (pistole == true) { if (Input.GetKeyDown(KeyCode.Alpha1)) { pistoleausgerüstet = true; damage = 10; unarmed = false; sturmgewehrausgerüstet = false; shotgunausgerüstet = false; } } if (sturmgewehr == true) { if (Input.GetKeyDown(KeyCode.Alpha2)) { sturmgewehrausgerüstet = true; damage = 20; pistoleausgerüstet = false; unarmed = false; shotgunausgerüstet = false; } } if (shotgun == true) { if (Input.GetKeyDown(KeyCode.Alpha3)) { shotgunausgerüstet = true; damage = 40; pistoleausgerüstet = false; unarmed = false; sturmgewehrausgerüstet = false; } } if (Input.GetMouseButton(0)) { if (assaultrilfeammo > 0) { ShootAssaultrilfe(); changeState(STATE_SHOOT); } else { asource.PlayOneShot(dryFireSound); } } if (Input.GetMouseButtonDown(0)) { if (ammo > 0) { ShootPistole(); } else { asource.PlayOneShot(dryFireSound); } if (Input.GetMouseButtonDown(0)) { if (shotgunammo > 0) { ShootShotgun(); } else { asource.PlayOneShot(dryFireSound); } } //ammo rilfe if (assaultrilfeammo == 0 && assaultrilfemagazin > 0) { assaultrilfemagazin--; assaultrilfeammo = 30; } } if (curHp <= 0) { GameOver(); } if (curHp > maxHp) { curHp = maxHp; } if (hasLaser) { // laser.active = true; } if (pistole == true) { damage = 10; shootDist = 150; } else if (sturmgewehr == true) { damage = 20; shootDist = 200; } else if (shotgun == true) { damage = 40; shootDist = 100; } } void Move() { Vector2 rigV = rig.velocity; if (Input.GetKey(KeyCode.LeftShift)) { moveSpeed = 150; } else { moveSpeed = 100; } //Player Movement if (Input.GetKey(KeyCode.W)) { changeState(STATE_WALK); rigV.y = moveSpeed; if (Time.time > walkrate + lastwalk) { asource.PlayOneShot(walksound); lastwalk = Time.time; } } else { changeState(STATE_IDLE); } if (Input.GetKey(KeyCode.A)) { changeState(STATE_WALK); rigV.x = -moveSpeed; if (Time.time > walkrate + lastwalk) { asource.PlayOneShot(walksound); lastwalk = Time.time; } } if (Input.GetKey(KeyCode.S)) { changeState(STATE_WALK); rigV.y = -moveSpeed; if (Time.time > walkrate + lastwalk) { asource.PlayOneShot(walksound); lastwalk = Time.time; } } if (Input.GetKey(KeyCode.D)) { changeState(STATE_WALK); rigV.x = moveSpeed; if (Time.time > walkrate + lastwalk) { asource.PlayOneShot(walksound); lastwalk = Time.time; } } rig.velocity = rigV; //Player Rotation Vector3 objectPos = new Vector3(0, 0, 0); Vector3 dir = new Vector3(0, 0, 0); objectPos = Camera.main.WorldToScreenPoint(transform.position); dir = Input.mousePosition - objectPos; transform.rotation = Quaternion.Euler(new Vector3(0, 0, Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90)); ; if (animator.GetCurrentAnimatorStateInfo(0).IsName("player_sheet_0")) { _isPlaying_walk = true; } else { _isPlaying_walk = false; } } void ShootPistole() { if (pistole == true && pistoleausgerüstet == true) { ammo--; RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.up, shootDist, mask.value); if (hit.collider != null) { hit.collider.gameObject.SendMessage("Damaged", damage); } asource.PlayOneShot(shootSound); GameObject mf = Instantiate(muzzleFlash, muzzle.transform.position, transform.rotation) as GameObject; mf.transform.parent = transform; GameObject.Destroy(mf, 0.1f); } } void ShootAssaultrilfe() { if (sturmgewehr == true && sturmgewehrausgerüstet == true) { if (Time.time > firerate + lastshot) { assaultrilfeammo--; RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.up, shootDist, mask.value); if (hit.collider != null) { hit.collider.gameObject.SendMessage("Damaged", damage); } asource.PlayOneShot(shootSound); GameObject mf_ar = Instantiate(muzzleFlash_ar, muzzle_ar.transform.position, transform.rotation) as GameObject; mf_ar.transform.parent = transform; GameObject.Destroy(mf_ar, 0.1f); lastshot = Time.time; } } } void ShootShotgun() { if (shotgun == true && shotgunausgerüstet == true) { shotgunammo--; RaycastHit2D hit = Physics2D.Raycast(transform.position, transform.up, shootDist, mask.value); if (hit.collider != null) { hit.collider.gameObject.SendMessage("Damaged", damage); } asource.PlayOneShot(shootgunSound); GameObject mf_sg = Instantiate(muzzleFlash_sg, muzzle_sg.transform.position, transform.rotation) as GameObject; mf_sg.transform.parent = transform; GameObject.Destroy(mf_sg, 0.1f); } } public void Damaged(int dmg) { curHp -= dmg; } void CameraFollow() { cam.transform.position = new Vector3(transform.position.x, transform.position.y, -10); } void GameOver() { if (PlayerPrefs.GetInt("Highscore") < Game.score) { PlayerPrefs.SetInt("Highscore", Game.score); } if (PlayerPrefs.GetInt("Highest Wave") < Game.curWave - 1) { PlayerPrefs.SetInt("Highest Wave", Game.curWave - 1); } Application.LoadLevel(0); } void OnGUI() { if (pistoleausgerüstet == true) { GUI.Box(new Rect(10, 10, 50, 50), "" + ammo); } else if (sturmgewehrausgerüstet == true) { GUI.Box(new Rect(10, 10, 100, 100), "" + assaultrilfeammo + " " + assaultrilfemagazin); } else if (shotgunausgerüstet == true) { GUI.Box(new Rect(10, 10, 100, 100), "" + shotgunammo); } else { GUI.Box(new Rect(10, 10, 100, 100), "0"); } GUI.Box(new Rect(10, 100, 50, 50), "" + curHp); } void changeState(int state) { if (_currentAnimationState == state) return; switch (state) { case STATE_WALK: animator.SetInteger("state", STATE_WALK); break; case STATE_SHOOT: animator.SetInteger("state", STATE_SHOOT); break; case STATE_IDLE: animator.SetInteger("state", STATE_IDLE); break; } _currentAnimationState = state; } void changeWeapon(int weapon) { if (_currentAnimationWeapon == weapon) return; switch (weapon) { case WEAPON_RILFE: animator.SetInteger("weapon", WEAPON_RILFE); break; case WEAPON_PISTOLE: animator.SetInteger("weapon", WEAPON_PISTOLE); break; } _currentAnimationWeapon = weapon; } } MfG Berlingames Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Wattsefakk Geschrieben 29. November 2015 Melden Share Geschrieben 29. November 2015 Hmm mit welcher Taste wechselst du die Waffen durch? finde das Nirgendswo in deinem Script oder bin ich blind... Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
SimpleScript Geschrieben 29. November 2015 Autor Melden Share Geschrieben 29. November 2015 Hi ho, Habs das Problem gelöst bekommen. Ich änderen die Animation einfach über einen externen Script, was das Problem scheinbar löst. mfG Berlingames Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Djeurissen Geschrieben 29. November 2015 Melden Share Geschrieben 29. November 2015 Öhh, viel wichtiger. Warum machst du das nicht generell so anstatt wirklich alles in ein Skript zu klatschen? Ich meine das Movement und das Waffenwechseln und Waffenstats verwalten muss ja nicht alles in ein Skript oder^^? Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
SimpleScript Geschrieben 29. November 2015 Autor Melden Share Geschrieben 29. November 2015 Jap das mit den externen Script ist mir halt dann auch eingefallen, das es die Sache wesentlich einfacher machen würde :-) Aber ich danke dir trotzdem für deine Hilfe. Link zu diesem Kommentar Auf anderen Seiten teilen More sharing options...
Recommended Posts
Archiviert
Dieses Thema ist jetzt archiviert und für weitere Antworten gesperrt.