What’s wrong here? I have this in the update function and I debuted the thing… it see’s the click but never does the if it even when it should be hitting…
if (Input.GetMouseButtonDown(0))// when button clicked...
{
gui = "no";
RaycastHit hit; // cast a ray from mouse pointer:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// if enemy hit...
if (Physics.Raycast(ray, out hit))
{
Debug.Log("selecting");
DeselectPlanet(); // deselect previous target (if any)...
selectedPlanet = hit.transform; // set the new one...
planetselect(); // and select it
}
}
I have this working in another script only thing I can think is it has to do with the fact that for this one the camera is in a empty object so I can move the camera. Also maybe since the camera is angled?
It is unclear what problem you are having. My guess is that your raycast is hitting something you don't expect. This results in 'selectedPlanet' getting set to some transform that is not a planet and therefore does not have the components you need. To check insert this line between lines 32 and 33. Debug.Log(hit.collider.tag+", "+hit.collider.name);
gui = "no";
}
RaycastHit hit; // cast a ray from mouse pointer:
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
// if enemy hit...
if (Physics.Raycast(ray, out hit))
{
Debug.Log("selecting");
DeselectPlanet(); // deselect previous target (if any)...
selectedPlanet = hit.transform; // set the new one...
planetselect(); // and select it
}
First, do you have just the one camera? If you have two cameras, you might want to use a reference to the camera inside your script instead of Camera.main (in general you might want to use it for performance benefits).
Second, I can recommend a perhaps simpler way to handle clicks on items: OnMouseUpAsButton. This method is invoked in any MonoBehaviour attached to an object that is clicked - meaning button is pressed and released on top of the object (as long as the object has a collider). You also have OnMouseDown to handle button pressed (without releasing), OnMouseDrag to handle holding the button down, OnMouseUp, etc.
Now, I would create a simple script that detects clicks on an object, and calls a method on your HUD script:
public class MapHud : MonoBehaviour {
// all your code here
// handle clicks on objects
public void ObjectClicked(ClickableObject obj) {
Debug.Log("selecting");
DeselectPlanet(); // deselect previous target (if any)...
selectedPlanet = obj.transform; // set the new one...
planetselect(); // and select it
}
}
public class ClickableObject : MonoBehaviour {
private MapHud hud;
public void Start() {
hud = Object.FindObjectOfType(typeof(MapHud)) as MapHud;
}
public void OnMouseUpAsButton() {
hud.ObjectClicked(this);
}
}
Then simply attach the ClickableObject script to any object you want selectable. Remember to make sure they all have colliders. In this case it doesn’t matter how many cameras you have.
Cool so now what if I want different actions for each clicked object? Where would I add this? In the clickableobject script… I would assume using tags? Wait… its still getting the transform so my tag system still works I think... Also I am getting a error saying: Assets/Scripts/ClickableObject.cs(10,13): error CS0266: Cannot implicitly convert type UnityEngine.Object' to MapHud'. An explicit conversion exists (are you missing a cast?)
Ok now working great just one more thing… I tried putting the gui = no in a else to make it deselect but no go. What am I missing? if(gui == "yes") { if(selectedPlanet.CompareTag("Sol System")) { Rect windowSize = new Rect(Screen.width - 300,Screen.height - 505,300,250); windowSize = GUI.Window(1, windowSize, Planet, ""); } else gui = "no"; }
I think your script is OK, it must be a problem with colliders. Adding a suitable collider to the object will solve your problem. Remember, you can only use the 3D collider, not 2D collider, these 2D colliders won’t work. Unity will fix this bug soon. I see someone has reported this to Unity.
If this is the right answer, please stick it for other people don’t have to look around for the answer.
Raycast, you can think that one is a special object, and it only deteches the collision with a collider. Make sure your collider is over the face of the object. You should change the code using Debug.DrawLine(ray.origin, hit.point); instead of using Debug.Log("selecting"); then you will see if your ray hits the object. If you don't see the ray, that mean there are some logic error in you project. That's all. Your code works well with me, I tried it. Unity 4.3 have a new physic system, for only 2D game. So I think can you have problem with them. If you don't, so not need to care about it.
It is unclear what problem you are having. My guess is that your raycast is hitting something you don't expect. This results in 'selectedPlanet' getting set to some transform that is not a planet and therefore does not have the components you need. To check insert this line between lines 32 and 33. Debug.Log(hit.collider.tag+", "+hit.collider.name);
– robertbuif (Physics.Raycast(ray, out hit)) what is out here
– Moor