- Home /
Angry Birds Style Loader (Load the Birds to be fired)
Hey guys. I'm trying to clone Angry Birds, more for practice than anything (I hate AB, Crush the Castle forever wins!), and so far I've got some basic blocks w/ physics, I can launch a single bird from the launch point and it reacts like it should. However, I can't get a loader for other birds to work.
I'm going at it like this: I have a loader object which, on startup, creates all birds based on an int number (3, 4, etc) and creates that many, about a space from each other, stored in a typed List in Loader. On each bird I have a mousefollow script that allows me to control it with the mouse (onmousedown, onmousedrag, onmouseup). I have a variable Active in the bird objects that let's unity know which one I'm trying to work with, but I can't seem to set the birds to active or not (simply changing the bool var Active). Among a few other things, this is the problem with my Loader.
My scripts of note: - FollowMouse (in a bird object) - Launcher (in a bird object) - Loader (in a loader object)
using UnityEngine; using System.Collections;
[RequireComponent(typeof(MeshCollider))]
public class FollowMouse : MonoBehaviour {
public GameObject Anchor;
public float MaxDistance;
public bool Active = true;
private bool isFree = false;
private Vector3 screenPoint;
private Vector3 offset;
private Vector3 tempPos;
public void SetActive(bool truefalse) {
Active = truefalse;
}
void OnMouseDown() {
if (Active == true && isFree == false)
{
screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
}
}
void OnMouseDrag() {
if (Active == true && isFree == false)
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
transform.LookAt(Anchor.transform.position, transform.up);
}
}
void OnMouseUp() {
if (Active == true && isFree == false)
{
Launcher code = GetComponent<Launcher>();
code.Launch(Anchor);
isFree = true;
Loader code2 = GetComponent<Loader>();
code2.Bird++;
}
}
void Update() {
if (Active == true && isFree == false)
{
if (Input.GetMouseButton(0))
{
if (Vector3.Distance(transform.position, Anchor.transform.position) > MaxDistance)
{
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
Vector3 diff = curPosition - Anchor.transform.position;
float distance = diff.magnitude;
transform.position = Anchor.transform.position + (diff / distance) * MaxDistance;
}
}
}
}
}
using UnityEngine; using System.Collections;
public class Launcher : MonoBehaviour {
private Vector3 LaunchForce;
public void Start() {
this.collider.enabled = false;
rigidbody.useGravity = false;
}
public void Launch(GameObject Anchor) {
float distance = Vector3.Distance(transform.position, Anchor.transform.position);
float ForceMod = distance / 3;
LaunchForce = transform.forward * (20 * ForceMod);
this.collider.enabled = true;
rigidbody.useGravity = true;
this.rigidbody.velocity = LaunchForce;
}
}
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class Loader : MonoBehaviour {
public int Birds;
public int Bird;
public GameObject prefab;
private List<GameObject> birds = new List<GameObject>();
private Vector3 pos;
// Use this for initialization
void Start () {
Bird = 0;
for (int i = 0; i < Birds; i++)
{
pos.Set(8 - i, 8, 0);
GameObject cube = (GameObject)Instantiate(prefab, pos, Quaternion.identity);
var temp = cube.GetComponent<FollowMouse>();
temp.Active = false;
temp.Anchor = GameObject.Find("LaunchPoint");
birds.Add(cube);
}
}
// Update is called once per frame
void Update () {
for (int i = 0; i < Birds; i++) {
if (i == Bird)
{
var temp = birds[Bird].GetComponent<FollowMouse>();
temp.SetActive(true);
}
else
{
var temp = birds[Bird].GetComponent<FollowMouse>();
temp.SetActive(false);
}
}
}
}
Please help me figure this one out!
I am trying to figure out how to do pretty much the same exact thing only I am using javascript. If anyone could help it would be much appreciated.
Came here looking for the same question to be answered. Shame that it's gone so long with no reply.
Your answer
Follow this Question
Related Questions
C# Gameobject Rigidbody Mouse Collision 1 Answer
How to refer to objects, scripts, textures in Assets in C#? 0 Answers
Array empties values on RunTime? 1 Answer
Can't access Depth of Field? 2 Answers
Left Click Script Problems 1 Answer