i have a game were bubbles fall to the ground i have a raycast script on the bubbles them self but when i try to destroy them it destroys the prefab instead of the clone that are made, if you could help i would really appreciate it very much.
CODE
function start()
{
if (bubbleEasy == true){
Health = 1;
}
else if (bubbleMed == true){
Health = 2;
}
else if (bubbleHard == true){
Health = 3;
}
}
function Update(){
for (var touch : Touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
// Construct a ray from the current touch coordinates
var ray = Camera.main.ScreenPointToRay(touch.position);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 400))
{
Health -= 1;
Debug.DrawLine(ray.origin, hit.point);
}
}
}
if (Health == 0){
PopBubble();
}
}
function OnMouseDown()
{
//Health -= 1;
//Destroy (gameObject);
}
function OnDestroy (){
tapCount += 1;
}
function PopBubble(){
Destroy (gameObject);
}
You’re doing the raycast in all bubbles, and whenever something is hit by the ray all bubbles decrement their Health variables. You could instead use collider.Raycast - this function ignores all other colliders, thus only the bubble hit will be affected:
...
var ray = Camera.main.ScreenPointToRay(touch.position);
var hit : RaycastHit;
if (collider.Raycast (ray, hit, 400))
{
Health -= 1;
Debug.DrawLine(ray.origin, hit.point);
}
...
But it would be way more efficient to do a single raycast in a script attached to the camera and call a function in the bubble script hit that would decrement its Health. The camera script could be like this:
function Update(){
for (var touch : Touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
// Construct a ray from the current touch coordinates
var ray = camera.ScreenPointToRay(touch.position);
var hit : RaycastHit;
if (Physics.Raycast (ray, hit, 400))
{
// call function DecHealth(1) in the bubble script:
hit.transform.SendMessage("DecHealth", 1, SendMessageOptions.DontRequireReceiver);
}
}
}
}
The bubble script would be much simpler:
function Start() // It's Start, not start!
{
if (bubbleEasy == true){
Health = 1;
}
else if (bubbleMed == true){
Health = 2;
}
else if (bubbleHard == true){
Health = 3;
}
}
function DecHealth(howMany: int)
{
Health -= 1;
if (Health <= 0){
Destroy(gameObject);
}
}
function OnDestroy (){
tapCount += 1;
}
NOTE: Start was written in lowercase in your code - it must start with a capital S, otherwise Unity won’t call it.
any idea how to limit to just 2 fingers?