Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Oobi23 · Apr 15, 2015 at 01:52 AM · newbiedestroy object

How to generate on click destroy enemies?

Hi there, I am not going to lie, I am relatively new to coding. So any help will be appreciated.

So what i require is quite simple. I need to keep generating constant enemies that come from all 4 sides of the game screen, also move across the game screen that would be killed off by a certain number of taps. So on click destroy.

How would I go about this?

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Olgo · Apr 15, 2015 at 02:26 AM

What you're looking to do is very basic and easily doable, however, it contains a few different parts you would need to look up. It would be hard to explain everything necessary in 1 answer.

I would highly, highly recommend doing a tutorial such as this:

http://unity3d.com/learn/tutorials/projects/survival-shooter

It will take a little bit of dedication but by the time you are done, you'll know 10x more than what you're asking in this question.

Good luck, have fun!

Comment
Add comment · Show 4 · 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 Oobi23 · Apr 15, 2015 at 03:33 AM 0
Share

thanks olgo.. I will be sure to go through the tutorial.. but anyway you could help me in co$$anonymous$$g up with a basic coding for a sphere, that would respawn as an enemy every few seconds? :O

avatar image Olgo · Apr 15, 2015 at 04:08 AM 0
Share

Sure why not

Copy this script in to a C# file named SphereEnemy. Add SphereEnemy to a GameObject. Drop that GameObject in your scene. Press Play.

I tried to comment each line as info-friendly as possible so you can glean something from it, hopefully it starts you down a path of awesome unity learning.

 using UnityEngine;
 using System.Collections;
 
 public class SphereEnemy : $$anonymous$$onoBehaviour {
 
     //declare a vector 3 to serve as a target
     Vector3 target;
 
     //how far will the ball be from the camera?
     int distFromCam = 5;
 
     //create a reference to a charactercontroller
     CharacterController cc;
 
     //movement speed multiplier for our enemy
     float moveSpeed = 5;
 
     // Use this for initialization
     void Start () {
         //set the target to be the middle of the screen according to the main camera
         target = Camera.main.ScreenToWorldPoint(new Vector3(Screen.width * .5f, Screen.height * .5f, distFromCam));
 
         //Tell us where sphere is headed
         Debug.Log (target.ToString ());
 
         //Add a character controller to this enemy, and save it to the variable "cc"
         cc = gameObject.AddComponent<CharacterController> ();
     }
     
     // Update is called once per frame
     void Update () {
         //turn sphere towards the target.
         transform.LookAt (target);
 
         //tell the character controller to move the sphere, direction of "forward" times speed times change in time (for smoothness)
         cc.$$anonymous$$ove (transform.forward * Time.deltaTime * moveSpeed);
     }
 
     void On$$anonymous$$ouseOver(){
         //When you left click on the sphere...
         if (Input.Get$$anonymous$$ouseButtonDown (0)) {
             Debug.Log("Sphere destroyed!");
 
             //transform of enemy is reset, looks like a new enemy is co$$anonymous$$g, this is a trick to save resources, and its faster for this example.
 
             //create a random integer from 0 to 10
             int randomPos = Random.Range( 0, 10 );
 
             //if the random divided by 2 is zero aka is it even or odd?
             if( randomPos % 2 == 0 ){
 
                 //if its even, set the position of this enemy to come from the bottom left + or - some random amount.
                 transform.position = Camera.main.ScreenToWorldPoint(new Vector3( 0, Screen.height * Random.Range( 0f, 1f ), distFromCam));
             }else{
 
                 //otherwise, set the position of this enemy to come from the top right, plus or $$anonymous$$us some random amount.
                 transform.position = Camera.main.ScreenToWorldPoint(new Vector3( Screen.width * Random.Range( 0f, 1f ), Screen.height, distFromCam));
             }
         }
     }
 }
avatar image Oobi23 · Apr 15, 2015 at 05:11 AM 0
Share

it works like a CHAR$$anonymous$$! thank you.. but everytime the enemy stops.. It starts glitching.. why is that?

avatar image Olgo · Apr 15, 2015 at 07:03 PM 0
Share

the reason you see the glitching is because the script tells the object every frame "look at your target, move X amount towards it"

that X amount it moves passes the target ever so slightly, so the next time the frame comes around the object has to turn around again towards the target and move a little bit towards it.

You can correct this by adding an if statement:

 if( Vector3.Distance( transform.position, target ) > .5f ){
   cc.$$anonymous$$ove (transform.forward * Time.deltaTime * moveSpeed);
 }

Add that if statemet around your cc.$$anonymous$$ove, it will stop the target from moving at .5f units away.

Another way you could do it, is to rotate the sphere slowly towards its target ins$$anonymous$$d of instantly. transform.LookAt() is instant, if you have a slow rotation, the sphere would slowly rotate around its target once it gets close to the destination

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Help with "Kaboom" game programming, Unity first time user. 1 Answer

How do you destroy a prefab, but not the original object? 1 Answer

Trying to use lists to check for available spawn points 2 Answers

the object is not destroyed to the preset value 1 Answer

Need Help on showing GUI Textures on click 0 Answers


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