using UnityEngine;
using System.Collections;
public class BackgroundPropSpawner04 : MonoBehaviour
{
public Rigidbody2D backgroundProp;
public float leftSpawnPosX; // The x coordinate of position if it's instantiated on the left.
public float rightSpawnPosX; // The x coordinate of position if it's instantiated on the right.
public float minSpawnPosY; // The lowest possible y coordinate of position.
public float maxSpawnPosY; // The highest possible y coordinate of position.public GameObject propInstance;
public float minSpeed; // The lowest possible speed of the prop.
public float maxSpeed; // The highest possible speeed of the prop.
public float minTimeBetweenSpawns; // The shortest possible time between spawns.
public float maxTimeBetweenSpawns; // The longest possible time between spawns.
//public bool facingLeft;
//public GameObject propInstance;
//public float speed = -10.0f;
void Start ()
{
// Set the random seed so it's not the same each game.
Random.seed = System.DateTime.Today.Millisecond;
//createSwan();
// Start the Spawn coroutine.
StartCoroutine("Spawn");
}
IEnumerator Spawn() {
//void createSwan() {
// Create a random wait time before the prop is instantiated.
float waitTime = Random.Range(minTimeBetweenSpawns, maxTimeBetweenSpawns);
// Wait for the designated period.
yield return new WaitForSeconds(waitTime);
// Randomly decide whether the prop should face left or right.
bool facingLeft = Random.Range(0,2) == 0;
// If the prop is facing left, it should start on the right hand side, otherwise it should start on the left.
float posX = facingLeft ? rightSpawnPosX : leftSpawnPosX;
// Create a random y coordinate for the prop.
float posY = Random.Range(minSpawnPosY, maxSpawnPosY);
// Set the position the prop should spawn at.
Vector3 spawnPos = new Vector3(posX, posY, transform.position.z);
Rigidbody2D propInstance = Instantiate(backgroundProp, spawnPos, Quaternion.identity) as Rigidbody2D;
if(!facingLeft)
{
// ... flip the scale in the x axis.
Vector3 scale = propInstance.transform.localScale;
scale.x *= -1; // scale.x = scale.x * -1;
propInstance.transform.localScale = scale;
}
// Create a random speed.
float speed = Random.Range(minSpeed, maxSpeed);
// These speeds would naturally move the prop right, so if it's facing left, multiply the speed by -1.
speed *= facingLeft ? -1f : 1f;
// Set the prop's velocity to this speed in the x axis.
propInstance.velocity = new Vector2(speed, 0);
// Restart the coroutine to spawn another prop.
StartCoroutine(Spawn());
while(propInstance != null)
{
if(facingLeft) {
if(propInstance.transform.position.x < leftSpawnPosX) {
// ... destroy the prop.
Destroy(propInstance.gameObject);
//createSwan();
}
}
else {
if(propInstance.transform.position.x > rightSpawnPosX) {
// ... destroy the prop.
Destroy(propInstance.gameObject);
//createSwan();
}
}
// Return to this point after the next update.
yield return null;
}
}
}
GameObject backgroundProp -> Rigidbody2D backgroundProp
[ 주의 사항 ] : backgroudnProp을 기존 소스에서 GameObject 형으로 사용한 후 소스를 Rigidbody2D로 바꾸게 되면 필히 여기에 대입했던 Object(swanSheet PrePab)를 다시 대입해야 함(그래야 Rigidbody형으로 받아오게 됨)
using UnityEngine;
using System.Collections;
public class Enemy03 : MonoBehaviour {
public float moveSpeed = 2f; // The speed the enemy moves at.
public int HP = 2; // How many times the enemy can be hit before it dies.
public Sprite deadEnemy; // A sprite of the enemy when it's dead.
public Sprite damagedEnemy; // An optional sprite of the enemy when it's damaged.
private SpriteRenderer ren; // Reference to the sprite renderer.
private Transform frontCheck; // Reference to the position of the gameobject used for checking if something is in front.
private bool dead = false; // Whether or not the enemy is dead.
// Use this for initialization
void Awake () {
// Setting up the references.
ren = transform.Find("body").GetComponent<SpriteRenderer>();
frontCheck = transform.Find("frontCheck").transform;
}
// Update is called once per frame
void FixedUpdate () {
// Create an array of all the colliders in front of the enemy.
Collider2D[] frontHits = Physics2D.OverlapPointAll(frontCheck.position, 1);
// Check each of the colliders.
foreach(Collider2D c in frontHits)
{
// If any of the colliders is an Obstacle...
if(c.tag == "Obstacle")
{
// ... Flip the enemy and stop checking the other colliders.
Flip ();
break;
}
}
// Set the enemy's velocity to moveSpeed in the x direction.
rigidbody2D.velocity = new Vector2(transform.localScale.x * moveSpeed, rigidbody2D.velocity.y);
// If the enemy has one hit point left and has a damagedEnemy sprite...
if(HP == 1 && damagedEnemy != null)
// ... set the sprite renderer's sprite to be the damagedEnemy sprite.
ren.sprite = damagedEnemy;
// If the enemy has zero or fewer hit points and isn't dead yet...
if(HP <= 0 && !dead)
// ... call the death function.
Death ();
}
public void Hurt()
{
// Reduce the number of hit points by one.
HP--;
}
void Death()
{
// Find all of the sprite renderers on this object and it's children.
SpriteRenderer[] otherRenderers = GetComponentsInChildren<SpriteRenderer>();
// Disable all of them sprite renderers.
foreach(SpriteRenderer s in otherRenderers)
{
s.enabled = false;
}
// Re-enable the main sprite renderer and set it's sprite to the deadEnemy sprite.
ren.enabled = true;
ren.sprite = deadEnemy;
// Set dead to true.
dead = true;
/*
// Find all of the colliders on the gameobject and set them all to be triggers.
Collider2D[] cols = GetComponents<Collider2D>();
foreach(Collider2D c in cols)
{
c.isTrigger = true;
}
*/
}
public void Flip()
{
// Multiply the x component of localScale by -1.
Vector3 enemyScale = transform.localScale;
enemyScale.x *= -1;
transform.localScale = enemyScale;
}
}
Start() -> Awake()
Update() -> FixedUpdate()
Enemy 밑의 body의 콤포넌트인 SpriteRenderer의 이미지 교체 부분
using UnityEngine;
using System.Collections;
public class Rocket04 : MonoBehaviour {
public GameObject explosion; // Prefab of explosion effect.
// Use this for initialization
void Start () {
}
void OnExplode()
{
// Create a quaternion with a random rotation in the z-axis.
Quaternion randomRotation = Quaternion.Euler(0f, 0f, Random.Range(0f, 360f));
// Instantiate the explosion where the rocket is with the random rotation.
Instantiate(explosion, transform.position, randomRotation);
}
// Update is called once per frame
void Update () {
//transform.Translate (new Vector3(0.1f, 0,0));
}
void OnTriggerEnter2D (Collider2D col)
{
print ("Trigger");
// If it hits an enemy...
if(col.tag == "Enemy")
{
// ... find the Enemy script and call the Hurt function.
col.gameObject.GetComponent<Enemy>().Hurt();
// Call the explosion instantiation.
OnExplode();
// Destroy the rocket.
Destroy (gameObject);
}
else if(col.gameObject.tag != "Player")
{
// Instantiate the explosion and destroy the rocket.
print("Explode ....");
OnExplode();
Destroy (gameObject);
}
}
}
using UnityEngine;
using System.Collections;
public class Enemy04 : MonoBehaviour {
public float moveSpeed = 2f; // The speed the enemy moves at.
public int HP = 2; // How many times the enemy can be hit before it dies.
public Sprite deadEnemy; // A sprite of the enemy when it's dead.
public Sprite damagedEnemy; // An optional sprite of the enemy when it's damaged.
public GameObject hundredPointsUI; // A prefab of 100 that appears when the enemy dies.
private SpriteRenderer ren; // Reference to the sprite renderer.
private Transform frontCheck; // Reference to the position of the gameobject used for checking if something is in front.
private bool dead = false; // Whether or not the enemy is dead.
// Use this for initialization
void Awake () {
// Setting up the references.
ren = transform.Find("body").GetComponent<SpriteRenderer>();
frontCheck = transform.Find("frontCheck").transform;
}
// Update is called once per frame
void FixedUpdate () {
// Create an array of all the colliders in front of the enemy.
Collider2D[] frontHits = Physics2D.OverlapPointAll(frontCheck.position, 1);
// Check each of the colliders.
foreach(Collider2D c in frontHits)
{
// If any of the colliders is an Obstacle...
if(c.tag == "Obstacle")
{
// ... Flip the enemy and stop checking the other colliders.
Flip ();
break;
}
}
// Set the enemy's velocity to moveSpeed in the x direction.
rigidbody2D.velocity = new Vector2(transform.localScale.x * moveSpeed, rigidbody2D.velocity.y);
// If the enemy has one hit point left and has a damagedEnemy sprite...
if(HP == 1 && damagedEnemy != null)
// ... set the sprite renderer's sprite to be the damagedEnemy sprite.
ren.sprite = damagedEnemy;
// If the enemy has zero or fewer hit points and isn't dead yet...
if(HP <= 0 && !dead)
// ... call the death function.
Death ();
}
public void Hurt()
{
// Reduce the number of hit points by one.
HP--;
}
void Death()
{
// Find all of the sprite renderers on this object and it's children.
SpriteRenderer[] otherRenderers = GetComponentsInChildren<SpriteRenderer>();
// Disable all of them sprite renderers.
foreach(SpriteRenderer s in otherRenderers)
{
s.enabled = false;
}
// Re-enable the main sprite renderer and set it's sprite to the deadEnemy sprite.
ren.enabled = true;
ren.sprite = deadEnemy;
// Set dead to true.
dead = true;
/*
// Find all of the colliders on the gameobject and set them all to be triggers.
Collider2D[] cols = GetComponents<Collider2D>();
foreach(Collider2D c in cols)
{
c.isTrigger = true;
}
*/
// Create a vector that is just above the enemy.
Vector3 scorePos;
scorePos = transform.position;
scorePos.y += 1.5f;
// Instantiate the 100 points prefab at this point.
Instantiate(hundredPointsUI, scorePos, Quaternion.identity);
}
public void Flip()
{
// Multiply the x component of localScale by -1.
Vector3 enemyScale = transform.localScale;
enemyScale.x *= -1;
transform.localScale = enemyScale;
}
}
죽는 적 캐릭터 머리 위에 100점 점수가 나오게 함
using UnityEngine;
using System.Collections;
public class Enemy05 : MonoBehaviour {
public float moveSpeed = 2f; // The speed the enemy moves at.
public int HP = 2; // How many times the enemy can be hit before it dies.
public Sprite deadEnemy; // A sprite of the enemy when it's dead.
public Sprite damagedEnemy; // An optional sprite of the enemy when it's damaged.
public AudioClip[] deathClips; // An array of audioclips that can play when the enemy dies.
public GameObject hundredPointsUI; // A prefab of 100 that appears when the enemy dies.
public float deathSpinMin = -100f; // A value to give the minimum amount of Torque when dying
public float deathSpinMax = 100f; // A value to give the maximum amount of Torque when dying
private SpriteRenderer ren; // Reference to the sprite renderer.
private Transform frontCheck; // Reference to the position of the gameobject used for checking if something is in front.
private bool dead = false; // Whether or not the enemy is dead.
private Score score; // Reference to the Score script.
// Use this for initialization
void Awake () {
// Setting up the references.
ren = transform.Find("body").GetComponent<SpriteRenderer>();
frontCheck = transform.Find("frontCheck").transform;
score = GameObject.Find("Score").GetComponent<Score>();
}
// Update is called once per frame
void FixedUpdate () {
// Create an array of all the colliders in front of the enemy.
Collider2D[] frontHits = Physics2D.OverlapPointAll(frontCheck.position, 1);
// Check each of the colliders.
foreach(Collider2D c in frontHits)
{
// If any of the colliders is an Obstacle...
if(c.tag == "Obstacle")
{
// ... Flip the enemy and stop checking the other colliders.
Flip ();
break;
}
}
// Set the enemy's velocity to moveSpeed in the x direction.
rigidbody2D.velocity = new Vector2(transform.localScale.x * moveSpeed, rigidbody2D.velocity.y);
// If the enemy has one hit point left and has a damagedEnemy sprite...
if(HP == 1 && damagedEnemy != null)
// ... set the sprite renderer's sprite to be the damagedEnemy sprite.
ren.sprite = damagedEnemy;
// If the enemy has zero or fewer hit points and isn't dead yet...
if(HP <= 0 && !dead)
// ... call the death function.
Death ();
}
public void Hurt()
{
// Reduce the number of hit points by one.
HP--;
}
void Death()
{
// Find all of the sprite renderers on this object and it's children.
SpriteRenderer[] otherRenderers = GetComponentsInChildren<SpriteRenderer>();
// Disable all of them sprite renderers.
foreach(SpriteRenderer s in otherRenderers)
{
s.enabled = false;
}
// Re-enable the main sprite renderer and set it's sprite to the deadEnemy sprite.
ren.enabled = true;
ren.sprite = deadEnemy;
// Increase the score by 100 points
score.score += 100;
// Set dead to true.
dead = true;
// Allow the enemy to rotate and spin it by adding a torque.
rigidbody2D.fixedAngle = false;
rigidbody2D.AddTorque(Random.Range(deathSpinMin,deathSpinMax));
// Find all of the colliders on the gameobject and set them all to be triggers.
Collider2D[] cols = GetComponents<Collider2D>();
foreach(Collider2D c in cols)
{
c.isTrigger = true;
}
// Play a random audioclip from the deathClips array.
int i = Random.Range(0, deathClips.Length);
AudioSource.PlayClipAtPoint(deathClips[i], transform.position);
// Create a vector that is just above the enemy.
Vector3 scorePos;
scorePos = transform.position;
scorePos.y += 1.5f;
// Instantiate the 100 points prefab at this point.
Instantiate(hundredPointsUI, scorePos, Quaternion.identity);
}
public void Flip()
{
// Multiply the x component of localScale by -1.
Vector3 enemyScale = transform.localScale;
enemyScale.x *= -1;
transform.localScale = enemyScale;
}
}