Results 1 to 10 of 10
  1. #1
    Join Date
    Oct 2011
    Posts
    6

    2d shooter tutorial issue:

    Hello

    I used part of the code from the 2d shooter for the collision between players and projectiles. I am having an issues with the enemy colliding with the player the explosion isn't instantiating or does the enemy go back to the start position-

    Here is the code I have for this-

    Code:
    using UnityEngine;
    using System.Collections;
    
    public class F35Player : MonoBehaviour {
    
    public Texture2D btnFire;
    public static int Score = 0;
    public static int Lives = 3;
    public GameObject AMRAM;
    public GameObject FriendlyExplosion;
    
    
    
       void OnGUI() 
            {
    		if (GUI.Button(new Rect(Screen.width/1.25f - 1,Screen.height/1.25f - 1, 50, 50), btnFire)) 
     		{
                Instantiate(AMRAM, transform.position, transform.rotation);
    	  	}
            GUI.Label(new Rect(100, 10, 100, 20), "Kills: " + F35Player.Score.ToString());
            GUI.Label(new Rect(100, 40, 100, 20), "Aircraft: " + F35Player.Lives.ToString());
            }
       void OnTriggerEnter(Collider otherObject)
    	    {
    		if (gameObject.tag == "Enemy")
    		{
    		   F35Player.Lives--;
    			
    		   Enemy enemy = (Enemy)otherObject.gameObject.GetComponent("Enemy");
    		   enemy.SetPositionAndSpeed();
    			
    		   Instantiate(FriendlyExplosion, transform.position, Quaternion.identity); 
    		}
    	}
    
    }

  2. #2
    Join Date
    Aug 2010
    Location
    Vector3.Zero
    Posts
    146
    I see two problems in your code. First of all, the Instantiated GameObject isnt getting moved, so the bullet won't move. To fix it you can create a list that can hold gameObjects, whenever a bullet is created assign its transform to the list. and a different function can update its position and destroy it if it goes out of bounce.

    Second when you say "gameObject.tag" it only takes the current gameObject's tag(which is a player, so it cannot have an enemy tag). To fix it change "gameObject.tag" to "otherObject.gameObject.tag".

    hope this helped, lemme know if u run into any further problems
    Noob stuff I've been playing with

    Multiplayer RPG - http://www.youtube.com/user/Stridervan?feature=mhee
    Advanced Minimap Tutorial - http://www.youtube.com/playlist?list...ature=view_all
    Website - http://projectzoso.weebly.com/

  3. #3
    Join Date
    Oct 2011
    Posts
    6
    Actually everything works fine sept for when the enemy hits the player it doesn't kill the player and it doesnt instan- the explosion or sends the enemy back to starting position- I checked everything the trigger sheck bosex the spelling all the stuff-

    I'll try your deal and let you know-

  4. #4
    Join Date
    Oct 2011
    Posts
    6
    Changing it to to otherObject.gameObject.tag didn't work-

  5. #5
    Join Date
    Aug 2010
    Location
    Vector3.Zero
    Posts
    146
    yes, the tag was whats causing it. Change the gameObject.tag to the colliders gameObject. gl
    Noob stuff I've been playing with

    Multiplayer RPG - http://www.youtube.com/user/Stridervan?feature=mhee
    Advanced Minimap Tutorial - http://www.youtube.com/playlist?list...ature=view_all
    Website - http://projectzoso.weebly.com/

  6. #6
    Join Date
    Oct 2011
    Posts
    6
    What do you mean- could you explain that alittle more-

    Thanks

  7. #7
    Join Date
    Oct 2011
    Posts
    6
    If you meant change the of(gameObject.tag =="") to (otherObject.tag)- I already didthat- sorry I should have realized that when I copy and pasted it- it says otherObject there- is that what you were referring to??

  8. #8
    Join Date
    Aug 2010
    Location
    Vector3.Zero
    Posts
    146
    if your still having troubles, do a debug statement like in

    Code:
    void OnTriggerEnter(Collider otherObject)
    {
    print("Outside the Conditional Check...");
    
    if(otherObject.gameObject.tag == "Enemy")
    {
    print("Inside the Conditional Check...");
    }
    }
    When you run the program,
    #1 None of the Debug statements fire off -> One or all of the colliders(player and enemy) doesnt have any colliders attached to them. If they already have colliders attached to them, its most likely the "Is Trigger" check isnt checked. If, you did all of em and still nothing, it means both colliders aren't colliding(check the alignment)

    #2 If the 1st debug statement fires off -> Meaning, that the enemy doesn't have a tag attached thats called "Enemy". Check the spelling(case-sensitive).

    #3 If both debug statements fires off -> This means, something wrong with the "enemy" scripts setPosition method and the explosion prefab is spawning somewhere in limbo.

    Quote Originally Posted by All_American View Post
    If you meant change the of(gameObject.tag =="") to (otherObject.tag)- I already didthat- sorry I should have realized that when I copy and pasted it- it says otherObject there- is that what you were referring to??
    And yes thats what I've ment

    Lemme know what happened
    Noob stuff I've been playing with

    Multiplayer RPG - http://www.youtube.com/user/Stridervan?feature=mhee
    Advanced Minimap Tutorial - http://www.youtube.com/playlist?list...ature=view_all
    Website - http://projectzoso.weebly.com/

  9. #9
    Join Date
    Oct 2011
    Posts
    6
    I figured it out- I had an empty gameobject that the projectile and explosion are connected to- so since they can hild a renderer I had to connect them right to the player- now it works-

  10. #10
    Join Date
    Aug 2010
    Location
    Vector3.Zero
    Posts
    146
    glad you've figured it out
    Noob stuff I've been playing with

    Multiplayer RPG - http://www.youtube.com/user/Stridervan?feature=mhee
    Advanced Minimap Tutorial - http://www.youtube.com/playlist?list...ature=view_all
    Website - http://projectzoso.weebly.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •