Ok guys I’m making a top-down space shooter adventure, I got the basic movement, bullets and enemies.
The player will be controlling the ship and the camera is scripted to the ship so it follows where ever it moves.
Part where I need help:
- Enemies collision
- - Power-ups system [REALLY REALLY NEED HELP) *
- - Bullets move slow while the ship is moving forward… I can’t seem to make the velocity of the ship + the bullet work…*
- - Enemy spawn, so far I got a individual spawn, I probably need the enemies to spawn from the camera…because this game is an open world adventure… but I need different waves of enemies spawning.*
- [/B]*
- About the enemies collision; the enemies are suppose to follow the player everywhere it goes and it does. The problem is that Unity’s physics engine doesn’t separate the enemies, that over time they will be on the same spot… and could be all killed by 1 bullet. How do I make it so that the enemies separates from each other and have space between them while still following the player?*
- About the power-ups… this is the real trouble for me, I got no idea how to tackle this.*
- Basically the power will gain EXP after killing enemies, [I could probably code this]*
- Once the player have enough EXP, they could unlock any power-ups at any order depending on which power-up they want.*
- Then the ship will release 4 random power-ups that the player can pick-up… it will maintain 4 power-ups at all time with a timer.*
- The power-up will stay within the camera (I could probably add a empty gameobject with collision on the corner of the camera).*
- Player AI code (movement/enemies follow/bullet/camera)*
- ```*
- *using UnityEngine;
using System.Collections;
public class AIscript : MonoBehaviour {
//game objects (variables which point to game objects)
private GameObject objPlayer;
private GameObject objCamera;
public Rigidbody rocketPrefab;
public Transform barrelEnd;
private Variables ptrScriptVariable;
//input variables (variables used to process and handle input)
private Vector3 inputRotation;
private Vector3 inputMovement;
//identity variables (variables specific to the game object)
public float moveSpeed = 100f;
private float enemySpeedUp = 5.0f;
private bool thisIsPlayer;
// calculation variables (variables used for calculation)
private Vector3 tempVector;
private Vector3 tempVector2;
private float tempSpeed;
// Use this for initialization
void Start () {
objPlayer = (GameObject) GameObject.FindWithTag ("Player");
objCamera = (GameObject) GameObject.FindWithTag ("MainCamera");
if (gameObject.tag == "Player") { thisIsPlayer = true; }
ptrScriptVariable = (Variables) objPlayer.GetComponent( typeof(Variables) );
}
// Update is called once per frame
void Update () {
FindInput();
ProcessMovement();
if (thisIsPlayer == true)
{
HandleCamera();
}
}
void FindInput ()
{
if (thisIsPlayer == true)
{
FindPlayerInput();
}
else
{
if(Time.time >= enemySpeedUp)
{
// Add 100 to the moveSpeed...
moveSpeed += 100f;
// make it wait another 5sec...
enemySpeedUp += 5.0f;
print (this.moveSpeed);
}
FindAIinput();
}
}
void FindPlayerInput ()
{
// find vector to move
inputMovement = new Vector3( Input.GetAxis("Horizontal"),0,Input.GetAxis("Vertical") );
// find vector to the mouse
tempVector2 = new Vector3(Screen.width * 0.5f,0,Screen.height * 0.5f); // the position of the middle of the screen
tempVector = Input.mousePosition; // find the position of the moue on screen
tempVector.z = tempVector.y; // input mouse position gives us 2D coordinates, I am moving the Y coordinate to the Z coorindate in temp Vector and setting the Y coordinate to 0, so that the Vector will read the input along the X (left and right of screen) and Z (up and down screen) axis, and not the X and Y (in and out of screen) axis
tempVector.y = 0;
inputRotation = tempVector - tempVector2; // the direction we want face/aim/shoot is from the middle of the screen to where the mouse is pointing
if(Input.GetButtonDown("Fire1"))
{
Rigidbody rocketInstance;
tempVector = Quaternion.AngleAxis(0.1f, Vector3.up) * inputRotation;
tempVector = (transform.position + (tempVector.normalized * 0.8f));
tempSpeed = 20.0f + rigidbody.velocity.magnitude;
rocketInstance = Instantiate(rocketPrefab,barrelEnd.position,Quaternion.LookRotation(inputRotation)/*barrelEnd.rotation*/) as Rigidbody;
rocketInstance.AddForce(barrelEnd.forward * tempSpeed);
rocketPrefab.transform.forward = transform.forward;
}
}
void FindAIinput ()
{
inputMovement = objPlayer.transform.position - transform.position;
inputRotation = inputMovement; // face the direction we are moving
}
void ProcessMovement()
{
tempVector = rigidbody.GetPointVelocity(transform.position) * Time.deltaTime * 1000;
rigidbody.AddForce (-tempVector.x, -tempVector.y, -tempVector.z);
rigidbody.AddForce (inputMovement.normalized * moveSpeed * Time.deltaTime);
transform.rotation = Quaternion.LookRotation(inputRotation);
transform.eulerAngles = new Vector3(0,transform.eulerAngles.y + 180,0);
transform.position = new Vector3(transform.position.x,0,transform.position.z);
}
void HandleCamera()
{
objCamera.transform.position = new Vector3(transform.position.x,5,transform.position.z);
objCamera.transform.eulerAngles = new Vector3(90,0,0);
}
void OnTriggerEnter(Collider whatHitMe)
{
if(whatHitMe.gameObject.tag=="Player")
{
print("Player Destroyed");
Destroy (whatHitMe.gameObject);
Destroy (this.gameObject);
}
}
// void removeMe ()
//{
//Destroy(gameObject);
//}
// void OnCollisionEnter(Collision Other)
// {
// if ( Other.gameObject.GetComponent( typeof(AIscript) ) != null &Other.gameObject != objPlayer ) // if we have hit a character and it is not the player
// {
// removeMe();
// }
// }
}**
- ```*
- Spawn Script*
- ```*
- *using UnityEngine;
using System.Collections;
public class spawnEnemies : MonoBehaviour
{
public Transform Enemy;
public float enemyNumber = 10f;
public float wave = 1f;
public float enemyCount = 0f;
private float timeSpawn = 3;
// Update is called once per frame
void Update ()
{
if (enemyNumber> enemyCount)
{
if(timeSpawn <= Time.time)
{
Instantiate(Enemy,transform.position, Quaternion.identity);
enemyCount +=1;
timeSpawn +=3;
}
}
}
}**
- ```*
- *
- As you can see the player ship is moving from AWSD, the aim is from the mouse pointer and the enemies are following the player - the problem is also clearly visible as the enemies are going inside each other and could be kill with 1 hit.*
- Enemy death script*
- ```*
- *using UnityEngine;
using System.Collections;
public class EnemyDeath : MonoBehaviour
{
//public GameObject ExplosionPrefab;
void OnTriggerEnter(Collider whatHitMe)
{
if(whatHitMe.gameObject.tag=="Bullet")
{
print("Enemy Destroyed");
Destroy (whatHitMe.gameObject);
Destroy (this.gameObject);
gameController.Score += 100;
}
}
}**
- ```*
- Most of this scripts are from tutorials as I’m still learning.*
- *About the power-up, I’m really stuck with this, if you could provide a base code or a tutorial… it would be very much appreciated.** - Regarding the enemy spawn system, should I just manually put spawn points everywhere? Or is there a better way to do this for this game type.*
- Please guys help me out, I’m new to Unity scripting, so tutorials similar to this game would be a great help.*
- Thank you very much for your time.*