Making enemies Attack?

Hello All,

After taking an extended break over the hotter months of summer - I’m back, with a whole set of new questions :grinning:. All in an effort to create a decent version of Laser Defender :sunglasses:. Yes, one of my question destined to frustrate you and leave you scratching your head wondering what I’m talking about :laughing:.

Right, I have a strange question to ask, but, I don’t really know how to go about asking it because I don’t know much about gaming terminology, so, I’ll explain what I’m doing as best I can, then get to the question … eventually.

I have been making the Laser Defender game and took some elements from Galaga. Keep in mind the elements are quite basic, because, I don’t really have the skills to do it justice. Now at certain intervals the individual elements of the formation, quickly move from the formation and with “creative” use of the animator move off the bottom of the screen and then reappear at the top going back into formation. Each enemy follows the same path because I don’t know any better, so, it’s a bit naff, but, it looks ok.

Anyway, now the question … I would like to implement a powerup type baddy. I have the powerup done and it works, but, I want this enemy to directly attack the ship. So, at whatever point the enemy attacks through a Random value + Time.time it moves into attack mode and directly targets the hero ship at that time - so the hero ship has to move otherwise it gets hit, so, it’s not continually targeting the hero ship - and then if the hero ship moves the baddy ship moves offscreen and then reappears at the top moving back into position. And if he get killed drops the powerup. So … any ideas on how to accomplish this because I don’t have any? Is this type of thing covered in any of the official Unity tutorials? Or, covered in any YouTube tutorials?

So, any hints, tips, links or any information you can give me would be greatly appreciated

Sorry for being so vague, but, I don’t know how to ask the question any better!

Thankyou for your patience.

Hey, writing from phone so expect some typing and other mistakes…

Basically you need this

Vector3 projectileDirection = new Vector3 playerShip.transform.position - this.transform.position;
GameObject projectile = Instantiate …;
Projectile.velocity += projectileDirection * speed * Time.deltaTime;

^ it will not work as it is (obviously you can’t instantiate bunch of dots, and variables/objects are not declared) but it can give you direction…

1 Like

Aha - now that gives me a great jumping off point :grinning:. Looking forward to seeing how I can use that information in the game context.

And, I totally got what you are doing with the … erm, dots :laughing:.

Thankyou very much and as I said it is greatly appreciated :sunglasses:.

:smiley:
I´m in front of my PC now so I can post real code :slight_smile:

You can see it in action here
http://gamebucket.io/game/0c5dbd90-034c-4a96-8d08-f85bc50cf818
after 40seconds enemies with this kind of attack starts to spawn

public GameObject rocket; // your projectile prefab 
public AudioClip rocketLaunched; // sound played when rocket is launched

private PlayerShip target; 
private Rigidbody2D rb;

void Start ()
{
    InvokeRepeating("LaunchRocket", 2f, 5f);    // You will have to invoke it in some sort of conditional update
}

void LaunchRocket()
{
	target = GameObject.FindObjectOfType<PlayerShip>(); // Finding target
	velocity = target.transform.position - this.gameObject.transform.position; // Calculating direction
	AudioSource.PlayClipAtPoint(rocketLaunched, transform.position); // Playing launch sound
	GameObject rocketProjectile = (GameObject) Instantiate(rocket, this.transform.position, Quaternion.identity) as GameObject; // Spawning rocket
	rocketProjectile.layer = 10; // Putting new rocket into right sorting layer 
	rocketProjectile.transform.rotation = Quaternion.LookRotation(velocity.normalized) * Quaternion.Euler(30,90,90); // Rotate rocket so it don't "slide"
	rb = rocketProjectile.GetComponent<Rigidbody2D>(); // Getting RigidBody from rocket
	rb.velocity = velocity.normalized * 300 * Time.deltaTime; // Addig speed to rocket, .normalized will recalculate vector to 1, so it doesn't depend from which distace is fired
}

hope this will help to solve rest of your questions

1 Like

You were assuming I could stay alive for 40 seconds :laughing:. That was a bit tough playing it at 1280*768. Anyway after I changed the screen res, I got to the mark you were talking about, I really like some of the ideas you had in your game, bravo!

Can you send me the exe file of your game for the desktop version? I would like to explore this game a bit more - a cross between Gradius & Asteriods :sunglasses:. You have the basis for a very addictive little game. Don’t send source files because I’ll just copy it :smile:.

Right, that code excerpt you sent, is going to help a great deal - what I am doing is slightly different but the main idea is the same. With some “jiggery-pokery”, I should be able to pull this of - “should” being the operative word.

Thanks again for your help, it has been MORE than helpful.

Beir bua agus beannacht!
Vaughan.

unfortunatly this is not possible right now… I am forced to rework scripts responsible for ugrades (currently shattered into three different scripts) because it becomes really impossible to maintain it and to add new items. But once it will work again I’ll let you know (only difference in gameplay so far from online build i’ve send to you is first miniboss with fully guided projectiles)

1 Like

Well, definitely keep me informed of your games progress because I really like some of the ideas you have included and would love to see how it ends up!

@Martin_Liska

I just wanted to tell you how helpful that code excerpt was. I now have the Baddy Ship that rotates to point toward the hero ship after a random length of time and then heads straight for it, which is exactly what I wanted. A few days ago when I first posed the question I didn’t think I would get a reply like yours which answered the question perfectly had a bit of trouble understanding how the Euler angles worked in the rotation code but it was just a matter of playing around with the values till I found out what worked.

The part that I thought would be the hardest is now done!

So, again I have to say thankyou!

1 Like

My pleasure, really happy I was able to help. Yeah Euler was sort of try and fail :smiley: should mention it, but my memory decided to delete this memory :smiley:

You can follow this ^^ topic, i publish it on itch as exe, it’s more time friendly

I tried using the link you posted:

And the result was a graphic telling me there was a: “404: not found” with text telling me that “We couldn’t find your page”. You might want to look into that to see if there is a problem occurring.

Anyway, I was having a play of the GameBucket version again, and did you use a store bought Inventory management system or did you code that yourself? And if you did code that yourself do you know of any tutorials that would teach how to implement that sort of thing! It’s not something I want to do know, but, in the future.

And the same for the Player Health status bar - do you know any tutorials for that? I used to use them in my GameMaker games because it was so easy but in Unity I’ve got no idea!

Regards,
Vaughan.

:confused: that’s weird, will looks it during day…

It’s coded… HP bar is UI slider (I googled for it, used part from unity tutorial, and some parts from one another, but don’t know which one)

Upgrade shop is my own, first version was real pain. Now I’m using Dictionary to store upgrade data, and whole buy/upgrade action is controlled from one method.
I’m using timeScale to pause game, there are a lot of people against it on the internet, but I can’t see any kind of problem in game like this…
Dictionary is pretty good tool, totally worth to learn and use.

Edit: Link should be working now, I didn’t publish the page for public

D’oh I should’ve realized that! I was watching a tutorial about slow motion not long ago on YouTube, but, never thought about putting the value down to zero. Thanks for pointing that out.

Dictionary is something I will look into because just about every game these days whether it be 2D/3D or using inventory. Including some of my most favourite games.

Thanks for all your help and guidance!

Regards,
Vaughan