I need some help with my scrolling shooter codes.
I have to move the collision detection to the enemy script, but I do not know enough C# to do it without running in to problems. Can anyone help me out?
These are the scripts I need to change:
Enemy
public class Enemy : MonoBehaviour {
#region varibles
public float MinSpeed;
public float MaxSpeed;
private float currentSpeed;
private float x, y, z;
private float MinRotateSpeed = 60f;
private float MaxRotateSpeed = 120f;
private float MinScale = .8f;
private float MaxScale = 2f;
private float currentRotationSpeed;
private float currentScaleX;
private float currentScaleY;
private float currentScaleZ;
#endregion
void Start () {
SetPositionAndSpeed();
}
void Update () {
float rotationSpeed = currentRotationSpeed * Time.deltaTime;
transform.Rotate(new Vector3(1, 0, 0) * rotationSpeed);
float amtToMove = currentSpeed * Time.deltaTime;
transform.Translate(Vector3.down * amtToMove, Space.World);
if (transform.position.y <= -6)
{
SetPositionAndSpeed();
}
}
public void SetPositionAndSpeed(){
currentRotationSpeed = Random.Range(MinRotateSpeed, MaxRotateSpeed);
currentScaleX = Random.Range(MinScale, MaxScale);
currentScaleY = Random.Range(MinScale, MaxScale);
currentScaleZ = Random.Range(MinScale, MaxScale);
currentSpeed = Random.Range(MinSpeed, MaxSpeed);
x = Random.Range(-6f, 6f);
y = 7.0f;
z =0.0f;
transform.position = new Vector3(x, y, z);
transform.localScale = new Vector3(currentScaleX, currentScaleY, currentScaleZ);
}
}
Player
public class Player : MonoBehaviour {
#region variables
public float PlayerSpeed;
public GameObject ProjectilePrefab;
public GameObject ExplosionPrefab;
// these vars have to do with player respawn
private float shipInvisibleTime = 1.5f;
private float shipMoveOnToScreenSpeed = 5;
private float blinkRate = .1f;
private int blinkCount;
private int numberOfTimesToBlink = 10;
#endregion
enum State{
Playing,
Explosion,
Invincible
}
private State state = State.Playing;
void Update () {
if (state != State.Explosion){
// Amount to move.
float amtToMove = Input.GetAxisRaw("Horizontal") * PlayerSpeed * Time.deltaTime;
// Move the player
transform.Translate(Vector3.right * amtToMove);
// Screenwrap
if (transform.position.x <= -7.5f)
transform.position = new Vector3(7.4f, transform.position.y, transform.position.z);
else if (transform.position.x >= 7.5f)
transform.position = new Vector3(-7.4f, transform.position.y, transform.position.z);
//create projectile
if (Input.GetKeyDown("space")){
Vector3 position = new Vector3(transform.position.x, transform.position.y + (transform.localScale.y / 2));
Instantiate(ProjectilePrefab, position, Quaternion.identity);
}
}
}
// if I hit something, this happens!
void OnTriggerEnter(Collider otherObject){
// this tells me Who i hit
if (otherObject.tag == "Enemy" state == State.Playing)
{
Controller.Lives -- ;
// this tells the enemy to reset
Enemy enemy = (Enemy)otherObject.gameObject.GetComponent("Enemy");
enemy.SetPositionAndSpeed();
// now kill me!
StartCoroutine(DestroyShip());
}
}
IEnumerator DestroyShip(){
state = State.Explosion;
Instantiate(ExplosionPrefab, transform.position, Quaternion.identity);
gameObject.renderer.enabled = false;
// center and move down
transform.position = new Vector3(0f, -6f, transform.position.z);
yield return new WaitForSeconds(shipInvisibleTime);
// if you are still alive then start blinking and move up
if (Controller.Lives > 0){
gameObject.renderer.enabled = true;
while (transform.position.y < -4){
float amtToMove = shipMoveOnToScreenSpeed * Time.deltaTime;
transform.position = new Vector3(0f, transform.position.y + amtToMove, transform.position.z);
yield return 0;
}
// while blinking you are invincible
state = State.Invincible;
while (blinkCount < numberOfTimesToBlink){
gameObject.renderer.enabled = !gameObject.renderer.enabled;
if (gameObject.renderer.enabled == true)
blinkCount ++;
yield return new WaitForSeconds(blinkRate);
}
blinkCount = 0;
// stop blinking
state = State.Playing;
}
// if you are dead then go to lose screen
else
Application.LoadLevel(2);
}
}
and Projectile.
public class Projectile : MonoBehaviour {
#region varibles
public float ProjectileSpeed;
public GameObject ExplosionPrefab;
private Transform myTransform;
#endregion
void Start () {
// we set this variable here so the cpu does not have to call up the position every frame
myTransform = transform;
}
void Update () {
// tell me how fast to Move
float amtToMove = ProjectileSpeed * Time.deltaTime;
// Move me up
myTransform.Translate(Vector3.up * amtToMove);
// kill me if i get to the top
if (myTransform.position.y > 5.4f){
Destroy(gameObject);
}
}
// when I hit something, this happens!
void OnTriggerEnter(Collider otherObject){
// this tells me Who i hit
if (otherObject.tag == "Enemy"){
// this tells the enemy to explode and reset
Enemy enemy = (Enemy)otherObject.gameObject.GetComponent("Enemy");
Instantiate(ExplosionPrefab, enemy.transform.position, enemy.transform.rotation);
enemy.SetPositionAndSpeed();
// now kill me!
Destroy(gameObject);
Controller.Score += 100;
if (Controller.Score >= 2000)
Application.LoadLevel(3);
}
}
}
I know it may be a lot to ask but I need to fix this before I can make any other enemy types, so I’m kinda stuck.
Please Help!!!