Last active
August 29, 2015 14:13
-
-
Save ymkjp/877e1d95b0b490c4ccf3 to your computer and use it in GitHub Desktop.
Live Training 13th June 2014 - Making an "Angry Birds" style game http://www.youtube.com/watch?v=L0tl0CwPYIc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| public class ProjectileDragging : MonoBehaviour { | |
| public float maxStretch = 3.0f; | |
| public LineRenderer catapultLineFront; | |
| public LineRenderer catapultLineBack; | |
| private SpringJoint2D spring; | |
| private Transform catapult; | |
| private Ray rayToMouse; | |
| private Ray leftCatapultToProjectile; | |
| private float maxStretchSqr; | |
| private float circleRadius; | |
| private bool clickedOn; | |
| private Vector2 preVelocity; | |
| void Awake () { | |
| spring = GetComponent <SpringJoint2D>(); | |
| catapult = spring.connectedBody.transform; | |
| } | |
| void Start () { | |
| LineRendererSetUp(); | |
| rayToMouse = new Ray(catapult.position, Vector3.zero); | |
| leftCatapultToProjectile = new Ray(catapultLineFront.transform.position, Vector3.zero); | |
| maxStretchSqr = maxStretch * maxStretch; | |
| CircleCollider2D circle = collider2D as CircleCollider2D; | |
| circleRadius = circle.radius; | |
| } | |
| void Update () { | |
| if (clickedOn) | |
| { | |
| Dragging (); | |
| } | |
| if (spring != null) | |
| { | |
| if (!rigidbody2D.isKinematic && preVelocity.sqrMagnitude > rigidbody2D.velocity.sqrMagnitude) { | |
| Destroy(spring); | |
| rigidbody2D.velocity = preVelocity; | |
| } | |
| if (!clickedOn) { | |
| preVelocity = rigidbody2D.velocity; | |
| } | |
| LineRendererUpdate (); | |
| } else | |
| { | |
| catapultLineFront.enabled = false; | |
| catapultLineBack.enabled = false; | |
| } | |
| } | |
| void LineRendererSetUp () { | |
| catapultLineFront.SetPosition(0, catapultLineFront.transform.position); | |
| catapultLineBack.SetPosition(0, catapultLineBack.transform.position); | |
| catapultLineFront.sortingLayerName = "Foreground"; | |
| catapultLineBack.sortingLayerName = "Foreground"; | |
| catapultLineFront.sortingOrder = 3; | |
| catapultLineBack.sortingOrder = 1; | |
| } | |
| void OnMouseDown () { | |
| spring.enabled = false; | |
| clickedOn = true; | |
| } | |
| void OnMouseUp () { | |
| spring.enabled = true; | |
| rigidbody2D.isKinematic = false; | |
| clickedOn = false; | |
| } | |
| void Dragging () { | |
| Vector3 mouseWorldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition); | |
| Vector2 catapultToMouse = mouseWorldPoint - catapult.position; | |
| if (catapultToMouse.sqrMagnitude > maxStretchSqr) | |
| { | |
| rayToMouse.direction = catapultToMouse; | |
| mouseWorldPoint = rayToMouse.GetPoint(maxStretch); | |
| } | |
| mouseWorldPoint.z = 0f; | |
| transform.position = mouseWorldPoint; | |
| } | |
| void LineRendererUpdate () | |
| { | |
| Vector2 catapultToProjectile = transform.position - catapultLineFront.transform.position; | |
| leftCatapultToProjectile.direction = catapultToProjectile; | |
| Vector3 holdPoint = leftCatapultToProjectile.GetPoint(catapultToProjectile.magnitude + circleRadius); | |
| catapultLineFront.SetPosition(1, holdPoint); | |
| catapultLineBack.SetPosition(1, holdPoint); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| public class ProjectileFollow : MonoBehaviour { | |
| public Transform projectile; | |
| public Transform farLeft; | |
| public Transform farRight; | |
| void Update () { | |
| Vector3 newPosition = transform.position; | |
| newPosition.x = projectile.position.x; | |
| newPosition.x = Mathf.Clamp(newPosition.x, farLeft.position.x, farRight.position.x); | |
| transform.position = newPosition; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| public class Resetter : MonoBehaviour { | |
| public Rigidbody2D projectile; | |
| public float resetSpeed = 0.025f; | |
| private float resetSpeedSqr; | |
| private SpringJoint2D spring; | |
| void Start () { | |
| resetSpeedSqr = resetSpeed * resetSpeed; | |
| spring = projectile.GetComponent <SpringJoint2D>(); | |
| } | |
| void Update () { | |
| if (Input.GetKeyDown(KeyCode.R)) | |
| { | |
| Reset (); | |
| } | |
| } | |
| void OnTriggerExit2D (Collider2D other) { | |
| if (other.rigidbody2D == projectile) | |
| { | |
| Reset (); | |
| } | |
| if (spring == null && projectile.velocity.sqrMagnitude < resetSpeedSqr) | |
| { | |
| Reset (); | |
| } | |
| } | |
| void Reset () { | |
| Application.LoadLevel(Application.loadedLevel); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using UnityEngine; | |
| using System.Collections; | |
| public class TargetDamage : MonoBehaviour { | |
| public int hitPoints = 2; | |
| public Sprite damagedSprite; | |
| public float damageImpactSpeed; | |
| private int currentHintPoints; | |
| private float damageImpactSpeedSqr; | |
| private SpriteRenderer spriteRenderer; | |
| void Start () { | |
| spriteRenderer = GetComponent <SpriteRenderer>(); | |
| currentHintPoints = hitPoints; | |
| damageImpactSpeedSqr = damageImpactSpeed * damageImpactSpeed; | |
| } | |
| void OnCollisionEnter2D (Collision2D collision) { | |
| if (collision.collider.tag != "Damager") | |
| { | |
| return; | |
| } | |
| if (collision.relativeVelocity.sqrMagnitude < damageImpactSpeedSqr) | |
| { | |
| return; | |
| } | |
| spriteRenderer.sprite = damagedSprite; | |
| currentHintPoints --; | |
| if (currentHintPoints <= 0) | |
| { | |
| Kill (); | |
| } | |
| } | |
| void Kill () { | |
| spriteRenderer.enabled = false; | |
| collider2D.enabled = false; | |
| rigidbody2D.isKinematic = true; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment