- Home /
Problems with clicking an object with my script
So I have a seriously dumb issue with my script where you have to click the bottom of the object (circle in this case) to get the object to disappear but your suppose to be able to click any part of the circle for it to disappear. There seems to be a problem with collision precision. By the way, this is built for mobile devices. Here is the code to give you an idea.
using UnityEngine;
using System.Collections;
public class FalloutBallScript : MonoBehaviour {
public float terminalVeclocity = -10;
private CircleCollider2D clickCollider;
private bool isBomb = false;
private int score = 1;
private float speed;
void Start()
{
clickCollider = gameObject.AddComponent<CircleCollider2D>();
clickCollider.radius = gameObject.GetComponent<CircleCollider2D>().radius * 1.4f;
clickCollider.isTrigger = true;
float randNum = Random.Range(0f, 1f);
if (randNum <= .1) isBomb = true;
if (isBomb)
{
renderer.material.color = Color.red;
gameObject.tag = "Bomb";
}
rigidbody2D.gravityScale = Random.Range(.85f, 1.1f);
Vector2 newVel = new Vector2(Random.Range(-1f, 1f), rigidbody2D.velocity.y);
speed = Random.Range(2f, 8f);
rigidbody2D.velocity = newVel * speed;
}
void FixedUpdate()
{
if (!FalloutGameControllerScript.gameOver)
{
foreach (Touch t in Input.touches)
{
Vector2 tPos = Camera.main.ScreenToWorldPoint(t.position);
if (t.phase == TouchPhase.Began)
{
if (clickCollider.bounds.Contains(tPos))
{
if (isBomb) FalloutGameControllerScript.gameOver = true;
else
{
FalloutGameControllerScript.score += score;
}
Destroy(gameObject);
}
}
}
Vector2 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
if (Input.GetMouseButtonDown(0))
{
if (clickCollider.bounds.Contains(mousePos))
{
if (isBomb) FalloutGameControllerScript.gameOver = true;
else
{
FalloutGameControllerScript.score += score;
}
Destroy(gameObject);
}
}
}
if (rigidbody2D.velocity.y < terminalVeclocity)
{
rigidbody2D.velocity = new Vector2(rigidbody2D.velocity.x, terminalVeclocity);
}
}
}
If you look at the colliders in Scene view while the app is running, are the colliders appropriate aligned with the object? I assume you are using an Orthographic camera?
Ya, I am using an Orthographic camera and the colliders seems to be perfectly aligned. I am honestly really confused.
You don't have multiple camera do you? Does it fail on both the touch device and with the mouse?
No, Only one camera per scene and it seems to work better with the mouse then it does with touch.
First, are you sure you want 'bounds.Contains' rather than 'OverlapPoint'? Since the collider is 2D, the bounds will have 0.0 for the 'z' extent. This is probably okay, but something you might want to look into if your problems persist.
Taking a closer look, is there any chance that it is not a collider issue but a lag between the mouse/finger hardware detection and the falling object position? Comment out lines 27 and 30, or drastically reduce the gravity and velocity. See if slow moving or not moving objects suffer from the same problem.
Your answer
Follow this Question
Related Questions
Obstacle avoidance 1 Answer
collision detection not working? 1 Answer
Collision detection for sprite 1 Answer
How does Unity detect collision? 0 Answers