Im having a problem with a small top down point and click game im trying to make. The code i have works just not how i need it too. Im very unfamiliar with raycasts and dont quite understand how they work. Right now if i click anywhere on the screen the object will be destroyed and particles will emit. Im trying to only make that happen whenever I click on said object. This script is attached to the game object i want destroyed when i click it. Any tips for me?
var particle : GameObject;
function Update () {
if(Input.GetMouseButton(0))
{
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray)) {
Instantiate (particle, transform.position, transform.rotation);
Destroy (gameObject);
Debug.Log("Pressed left click.");
}
}
}
I assume you mean the object this script is attached to? I probably wouldn’t do it that way, because if you plan on having more than one object with this same script, you don’t want a hundred scripts running monitoring every frame for mouse input… I’d probably put this script on the camera or something and just use tags on the objects I want to be destroyable with the mouse click. eg:
var range : float = 100.0;
var particle : GameObject;
function Update () {
if(Input.GetMouseButton(0))
{
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var hit : RaycastHit;
if (Physics.Raycast(ray, hit, range)){
if(hit.gameObject.CompareTag("Enemy"){
Instantiate (particle, hit.transform.position, hit.transform.rotation);
Destroy(hit.gameObject);
}
}
}
}
Quick explanation. Think of a ray as a origin point, and a heading. The heading is given as a normal. so Vector3.up, is Vector3(0,1,0), meaning that up is Y positive.
OK, for your code, you are creating a ray, ScreenPointToRay. This creates a point for all intensive purposes at the position (mousePosition) and faces it straight from lower clip plane away from the actual position of the camera. (this really isn’t where your problem is)
In order to now see where your problem is let me redo your code just a bit. We need to figure out what we clicked on and if that something is actually destroyable.
var particle : GameObject;
function Update () {
if(Input.GetMouseButton(0))
{
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
[COLOR="red"]var hit : RaycastHit;
if (Physics.Raycast (ray, hit)) {
if(hit.collider.gameObject.tag=="Destroyable"){[/COLOR]
Instantiate (particle, transform.position, transform.rotation);
Destroy (hit.collider.gameObject);
Debug.Log("Pressed left click.");
}
}
}
}
thank you I will try this script out. And your right I didnt think about the script running multiple times. Its telling me Assets/PointClick.js(10,24): BCE0019: ‘gameObject’ is not a member of ‘UnityEngine.RaycastHit’.
Ah, true, you would want to use GetMouseButtonDown, not GetMouseButton
Legend - That error is what i got with your code.
BigMisterB im having a problem when i use prefabs or duplicates of the objects im destroying. When ever i add multiple objects and destroy one of them. A particle effect shows up at every one