Skip to content

Instantly share code, notes, and snippets.

@JonathanYin
Last active July 20, 2017 17:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JonathanYin/909d5034fde2ff5c14e4e03d2f14fcd9 to your computer and use it in GitHub Desktop.
Save JonathanYin/909d5034fde2ff5c14e4e03d2f14fcd9 to your computer and use it in GitHub Desktop.
using UnityEngine;
public class MoveScript : MonoBehaviour
{
public Vector2 speed = new Vector2(10, 10);
public Vector2 direction = new Vector2(-1, 0);
private Vector2 movement;
private Rigidbody2D rigidbodyComponent;
public static int lives = 3;
void Update()
{
// 2 - Movement
movement = new Vector2(
speed.x * direction.x,
speed.y * direction.y);
}
void FixedUpdate()
{
if (rigidbodyComponent == null) rigidbodyComponent = GetComponent<Rigidbody2D>();
// Apply movement to the rigidbody
rigidbodyComponent.velocity = movement;
}
void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Player1")
{
//other.GetComponent<Renderer>().enabled = false;
//other.gameObject.SetActive(false);
/*if (Global.count <= 0)
{
Destroy(other.gameObject);
}
else
{
Global.count = Global.count - 1;
Global.DisplayHearts();
}*/
SpecialEffectsHelper.Instance.Explosion(transform.position); //EXPLOSION!!!
Destroy(other.gameObject);
}
else if (other.tag == "Player2")
{
//other.GetComponent<Renderer>().enabled = false;
//other.gameObject.SetActive(false);
/*if (Global.count <= 0)
{
Destroy(other.gameObject);
}
else
{
Global.count = Global.count - 1;
Global.DisplayHearts();
}*/
SpecialEffectsHelper.Instance.Explosion(transform.position); //EXPLOSION!!!
Destroy(other.gameObject);
}
}
}
@JonathanYin
Copy link
Author

This script is attached to an Enemy in the game, which is spawned from a BombSpawner and moves at a consistent pace across the screen. If a player collides with it, the player is killed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment