Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by husnain_rao · Jan 11, 2018 at 05:58 PM · ballspringjointinitialisation

how to intialize ball?

Hey Friends I am working on a angry bird like game. In which i have used a spring joint and many game objects(balls) initilises one by one. i am not happy with it i want to initialize a ball by loop every time through the ball. please guide me thanx

Comment
Add comment · Show 1
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 husnain_rao · Jan 12, 2018 at 10:29 AM 0
Share

Nothing Happend. Ball Just Falling to Bottom i want it to through using spring joint Here is my code the ball using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Scene$$anonymous$$anagement; using UnityEngine.UI;

 public class Ball : $$anonymous$$onoBehaviour {
     public AudioClip Hit,leave;
     private bool isPressed = false;
     public Rigidbody2D rb;
     public Rigidbody2D Holder;
     public float releaseTime = .15f, loadtime;
     public float maxDragDistance= 2.5f;
     public GameObject nextBall;
     public Text text;
     public static int remaininghits=10;
     public Level$$anonymous$$anager levelmanager;
     public TextControll textcontroll;
     GameObject prefab;
     // Use this for initialization
     void Start(){
         prefab = Resources.Load ("Projectile") as GameObject;
         levelmanager = GameObject.FindObjectOfType<Level$$anonymous$$anager> ();
         levelmanager = GameObject.FindObjectOfType<Level$$anonymous$$anager> ();
     }
     void Update(){
         if (isPressed) {
             //capture mouse pos;
             Vector2 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             if (Vector3.Distance (mousePos, Holder.position) > maxDragDistance)
                 rb.position = Holder.position + (mousePos - Holder.position).normalized * maxDragDistance;
                 else
                 rb.position = mousePos;
         }
     }
     void OnCollisionEnter2D(Collision2D Collision){
         AudioSource.PlayClipAtPoint (Hit, transform.position);
         Debug.Log ("Collision Detected");
     }
     void On$$anonymous$$ouseDown () {
         isPressed = true;
         rb.is$$anonymous$$inematic = true;
     }
     void On$$anonymous$$ouseUp () {
         //on leaving the mouse
         isPressed = false;
         rb.is$$anonymous$$inematic = false;
         remaininghits--;
         StartCoroutine(Release ());
     }
     IEnumerator Release(){
         AudioSource.PlayClipAtPoint (leave, transform.position);
         yield return new WaitForSeconds (releaseTime);
         GetComponent<SpringJoint2D> ().enabled = false;
         this.enabled = false;
          //New ball
         yield return new WaitForSeconds (2f);
         if (nextBall != null) {
             nextBall.SetActive (true);
             Destroy (gameObject, 3f);
         } 
         else {
             yield return new WaitForSeconds (loadtime);
             remaininghits=10;
             Application.LoadLevel ("start");
         }    
     }
 }
 

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by haroldgeronimo · Jan 11, 2018 at 07:56 PM

You can create a prefab by dragging and dropping the ball on the project folder and use the Instantiate method inside a loop to "initialize" it on your game .

 public GameObject ball // Set this value in the inspector by dragging and dropping the prefab
 
 void Start(){
 
 GameObject ballObject; // a temporary holder for the instantiated object
 
 for(i=0;i<length;i++){ // loop for instantiating balls
 
 ballObject = Instantiate(ball); // instantiates a new prefab ball

 ballObject.Transform.position = new Vector2(x,y) /*change x and y with the position where you want the balls to be located (you can use i to have consistent variation of location or a random range for random location)*/


 //add other components here using the ballObject if you wish
 }
 
 
 }



Comment
Add comment · Share
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
0

Answer by husnain_rao · Jan 12, 2018 at 10:30 AM

Nothing Happend. Ball just Falling to Bottom i want it to through with spring joint Here is my code the ball.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 using UnityEngine.UI;
 public class Ball : MonoBehaviour {
     public AudioClip Hit,leave;
     private bool isPressed = false;
     public Rigidbody2D rb;
     public Rigidbody2D Holder;
     public float releaseTime = .15f, loadtime;
     public float maxDragDistance= 2.5f;
     public GameObject nextBall;
     public Text text;
     public static int remaininghits=10;
     public LevelManager levelmanager;
     public TextControll textcontroll;
     GameObject prefab;
     // Use this for initialization
     void Start(){
         prefab = Resources.Load ("Projectile") as GameObject;
         levelmanager = GameObject.FindObjectOfType<LevelManager> ();
         levelmanager = GameObject.FindObjectOfType<LevelManager> ();
     }
     void Update(){
         if (isPressed) {
             //capture mouse pos;
             Vector2 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
             if (Vector3.Distance (mousePos, Holder.position) > maxDragDistance)
                 rb.position = Holder.position + (mousePos - Holder.position).normalized * maxDragDistance;
                 else
                 rb.position = mousePos;
         }
     }
     void OnCollisionEnter2D(Collision2D Collision){
         AudioSource.PlayClipAtPoint (Hit, transform.position);
         Debug.Log ("Collision Detected");
     }
     void OnMouseDown () {
         isPressed = true;
         rb.isKinematic = true;
     }
     void OnMouseUp () {
         //on leaving the mouse
         isPressed = false;
         rb.isKinematic = false;
         remaininghits--;
         StartCoroutine(Release ());
     }
     IEnumerator Release(){
         AudioSource.PlayClipAtPoint (leave, transform.position);
         yield return new WaitForSeconds (releaseTime);
         GetComponent<SpringJoint2D> ().enabled = false;
         this.enabled = false;
          //New ball
         yield return new WaitForSeconds (2f);
         if (nextBall != null) {
             nextBall.SetActive (true);
             Destroy (gameObject, 3f);
         } 
         else {
             yield return new WaitForSeconds (loadtime);
             remaininghits=10;
             Application.LoadLevel ("start");
         }    
     }
 }
 
Comment
Add comment · Share
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

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

74 People are following this question.

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

Related Questions

Simulating Marble Blast Gold: A script to make a character as a marble roll forward, mouse turns screen? 2 Answers

Whats the JavaScript for making a ball roll? 2 Answers

Simple Collision Deflection (Pong) 1 Answer

How to show a scored goal? 1 Answer

Simple peggle type ball physics 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