2016-11-28 4 views
0

Ich bin extrem nah dran, mein Spiel zu beenden, jeder Code, den ich gepostet habe, funktioniert gut, aber ich möchte, dass mein Spieler stirbt, egal, ob er mit der Box kollidiert (was der Feind ist). Ich habe jedoch versucht, etwas zu recherchieren, und ich kann nicht die Lösung finden. Wie mache ich das? Hier ist der Code für den Player (JugadorScript.cs):Einheit: Spieler Tod bei Objektkollision

using UnityEngine; 
using System.Collections; 

public class JugadorScript : MonoBehaviour 
{ 

    public float velocidad = -10f; 
    void Start() 
    { 

    } 

    // Update is called once per frame 
    void Update() 
    { 

    } 
    public void moverIzquierda() 
    { 
     transform.Translate(Vector2.right * velocidad * Time.deltaTime); 
     transform.eulerAngles = new Vector2(0, 0); 
    } 
    public void moverDerecha() 
    { 
     transform.Translate(Vector2.right * velocidad * Time.deltaTime); 
     transform.eulerAngles = new Vector2(0, 180); 
    } 
} 

Der EnemySpawner.cs Code, der ausgezeichnete funktioniert:

using UnityEngine; 
using System.Collections; 
using System; 

public class EnemySpawner : MonoBehaviour 
{ 

    public GameObject BlockPrefab; 

    float maxSpawnRateInSeconds = 2.5f; 

    void Start() 
    { 
     Invoke("SpawnEnemy", maxSpawnRateInSeconds); 
     InvokeRepeating("IncreaseSpawnRate", 0f, 30f); 
    } 

    // Update is called once per frame 
    void Update() 
    { 

    } 
    void SpawnEnemy() 
    { 
     Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)); 

     Vector2 max = Camera.main.ViewportToWorldPoint(new Vector2(1, 1)); 

     GameObject anEnemy = (GameObject)Instantiate(BlockPrefab); 
     anEnemy.transform.position = new Vector2(UnityEngine.Random.Range(min.x, max.x), max.y); 


     ScheduleNextEnemySpawn(); 
    } 

    void ScheduleNextEnemySpawn() 
    { 
     float spawnInNSeconds; 

     if (maxSpawnRateInSeconds > 1f) 
     { 
      spawnInNSeconds = UnityEngine.Random.Range(1f, maxSpawnRateInSeconds); 
     } 
     else 
      spawnInNSeconds = 1f; 

     Invoke("SpawnEnemy", spawnInNSeconds); 
    } 

    void IncreaseSpawnRate() 
    { 
     if (maxSpawnRateInSeconds > 1f) 
      maxSpawnRateInSeconds--; 

     if (maxSpawnRateInSeconds == 1f) 
      CancelInvoke("IncreaseSpawnRate"); 
    } 


} 

Und die BlockScript.cs, die mein Feind Skript ist:

using UnityEngine; 
using System.Collections; 

public class BlockScript : MonoBehaviour 
{ 

    private GameObject wayPoint; 
    private Vector3 wayPointPos; 
    private Rigidbody2D rigidBody2D; 
    public bool inGround = true; 
    private float jumpForce = 400f; 

    private float speed = 6.0f; 
    void Start() 
    { 

     wayPoint = GameObject.Find("wayPoint"); 
    } 

    private void awake() 
    { 
     rigidBody2D = GetComponent<Rigidbody2D>(); 
    } 


    void Update() 
    { 

     if (inGround) 
     { 
      inGround = false; 

      rigidBody2D.AddForce(new Vector2(0f, jumpForce)); 
     } 

     wayPointPos = new Vector3(wayPoint.transform.position.x, transform.position.y, 
      wayPoint.transform.position.z); 

     transform.position = Vector3.MoveTowards(transform.position, 
      wayPointPos, speed * Time.deltaTime); 

     Vector2 min = Camera.main.ViewportToWorldPoint(new Vector2(0, 0)); 

     if (transform.position.y < min.y) 
     { 
      Destroy(gameObject); 
     } 



    } 


} 
+2

Beachten Sie, dass MonoBehaviour Methoden case-sensitive sind! Z.B. 'wach()! = wach()'. Dies wird Ihnen Kopfschmerzen ersparen. – Serlite

Antwort

Verwandte Themen