Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Loathing · Jul 28, 2011 at 12:17 AM · collisiondestroyfloating-text

Collisions between clones, text/numbers, spawn control.

Hello. I've been going through search after search on this site looking for anything that would help me with my problem, could be I don't know exactly what to look for but I haven't found much. I'm working on a "defense" style game and have run into a few speed bumps, first the script I wrote to destroy these spawned meteors doesn't work. I did find a similar question and the answer was to target the Clone object, not really sure how to do that...


    function OnCollisionEnter(onHit : Collision)
    {
        if(onHit.gameObject.name == "meteorPrefab(Clone)")
           {
              Destroy(gameObject.name("meteorPrefab(Clone)");
           }
    }
This code is the best I've been able to come up with from tutorials and such. Does it matter that I've got eight spawn points all spawning the meteorPrefab object at the same time and the method I'm trying to use will end up destroying all eight instantiated meteorPrefabs?

Also trying to create a HUD like element. The idea is basically a floating name above the meteor, with with a mix of numbers and letters. The game is played from a top-down perspective and thus I thought a billboard with a lookAt script would be unnecessarily call heavy so I opted for a simple plane level on the X,Z axes. However the play stays with the meteors local Z+ axis and I would like to clamp it to the world Z+, and lock the rotations so no matter how the meteor moves/rotates the plane stays above it on the screen. The plane has a sort of targeting reticule texture on it and I would like the text/numbers to be displayed over this (another plane layered on top?).

As for the spawn control part of the title, I want only one meteor to be spawned from any one spawn point at a time, being that they're moving in a single direction I figured I'd use raycasting to just check if the meteor object is there, and once it's not spawn a new one. Heh, testing this idea has to wait til after I can destroy the meteors. Thanks in advance and sorry for the wordy post.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image FreeTimeDev · Jul 28, 2011 at 02:37 AM 0
Share

One question per post please!

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by FreeTimeDev · Jul 28, 2011 at 02:43 AM

Would using tags (and .tag, instead of .name) work better for you? I don't know your circumstances and how cumbersome that could make the programming but that might be something you want to look into. (Giving the meteors a meteor tag, or a tag they can share with something else, and checking to see if the collision object has the tag).

I'm not sure I see a question regarding the floating health bar. Maybe you can make it a Cube with a rigidbody and lock it's rotation in the rigidbody constraints?

Spawning: I'm actually working on a top down game with a ton of spawning and I'm going to very much advise against ray casting for spawning. There are a lot of ways to do spawning but the easiest would probably just to keep track of spawning in a global variable and to check it occasionally when spawning.

There are a dozen ways to do this and I'm not going to reinvent the wheel when I myself started my project with the help of many other answered questions here. Spawning has been answered pretty heavily (especially random spawning).

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by aldonaletto · Jul 28, 2011 at 03:58 AM

1- Comparing clone names is not a good practice. It would be better to tag the meteor prefab as "meteor" and check the tag in OnCollisionEnter. The way to destroy the hit object is using onHit.transform.gameObject, like below:

 function OnCollisionEnter(onHit : Collision){
   if(onHit.gameObject.tag == "meteor"){
     Destroy(onHit.transform.gameObject);  // that's the way to kill the hit object 
   }
 }
Don't mind about the other meteors, only the one which hit the collider will be destroyed.

2- Maybe you should use a GUIText to follow the meteor. There's a script from @Eric5h5 (ObjectLabel.js, click here to get it) which associates the GUIText to some object, and also includes an offset parameter to keep the text at a fixed distance. You can create a GUIText, adjust font, size, style, anchor etc, add Eric's script to it and save as a prefab. When instantiating the meteor, instantiate also the GUIText and set its target variable to the meteor - more on this below.

3- There's an easiest way to avoid launching a meteor while the last one is still alive: child the meteor to the spawning point object right after instantiation and verify transform.childCount - it will count the child meteor while it's alive, and return to zero when it's destroyed. The script below (to be attached to the spawning point object) only creates a new meteor when there's no child. Additionally, it creates the text and associates the meteor to it.

  if (transform.childCount == 0){ // only create a meteor if the previous was destroyed
    // create the meteor
    var meteor: Transform = Instantiate(meteorPrefab,transform.position,transform.rotation);
    meteor.parent = transform; // child the meteor to the spawn point object
    // create the text to follow the meteor
    var text: Transform = Instantiate(textPrefab,Vector3.zero,Quaternion.identity);
    text.GetComponent(ObjectLabel).target = meteor; // associate the meteor to the label
  }
Comment
Add comment · Show 12 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Loathing · Jul 31, 2011 at 02:19 AM 0
Share

@$$anonymous$$atthew I tried the tag method as well to no avail.

Aldon I'll try your suggestions and let you know what the outcome is. Thanks :D

avatar image aldonaletto · Jul 31, 2011 at 02:28 AM 0
Share

Let me warn you about a potential problem: the spawning point object (the one which instantiated the meteor) must not move or rotate while the meteor is alive - since the meteor was childed to it, if the object rotates, the meteor direction will be affected too.

avatar image Loathing · Jul 31, 2011 at 02:44 AM 0
Share

Well then it's a good thing I've already decided that they spawns are fixed.

avatar image aldonaletto · Jul 31, 2011 at 02:56 AM 0
Share

Wise decision!

avatar image Loathing · Aug 07, 2011 at 10:14 PM 0
Share

I apologize for not my lack of understanding in trouble shooting but I'm getting an error:

 $$anonymous$$eteorSpawn.js(17,34): BCE0023: No appropriate version of 'UnityEngine.Component.GetComponent' for the argument list '(UnityEngine.Transform)' was found. 
one answer I found had to do with returning one game object and then the code was trying to return that same game object through a plural call.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[SIMPLE] Not loose speed when destroying colliding object 1 Answer

Collision Destroy script without trigger 0 Answers

Randomly keeping paintball intact on collision 1 Answer

Can prefabs interact with scroll rect? 0 Answers

Zombie Health Script Not working?? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges