- Home /
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?
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!
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
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));
}
}
}
}
it works like a CHAR$$anonymous$$! thank you.. but everytime the enemy stops.. It starts glitching.. why is that?
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