Hey Friends!
I am working on a game and in which i want to through the ball using a spring joint. i want to through the ball n times but i don’t want to create objects manualy.
Is there any way to create objects using Script.
i am using this code for my ball.
Thanx
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;
// Use this for initialization
void Start(){
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");
}
}
}