Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Kadaiyen · Mar 05, 2012 at 02:57 AM · c#objectmousevariable

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!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SBull · Sep 28, 2013 at 10:56 PM 0
Share

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.

avatar image Mkalafut · Jan 30, 2016 at 05:28 PM 0
Share

Came here looking for the same question to be answered. Shame that it's gone so long with no reply.

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges