Quantcast
Channel: Answers for "Making Object Clickable(Need Guidance ASAP)"
Viewing all articles
Browse latest Browse all 4

Answer by aldonaletto

0
0
Physics.Raycast checks if the logical ray passed as argument hits some collider. If so, it returns true and fills the RaycastHit structure with info about the object. The object must have a collider, or it won't work. In your case, many things may be going wrong: the collider may be too small, or badly positioned, or something else is in front of it and blocking the building collider. You can change the script to a simpler thing like below, where the name of the clicked object will be returned - this may help you to understand what's exactly going on:
function Update () {	if (Input.GetMouseButton(0)) {		var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);		var hit: RaycastHit;				if (Physics.Raycast(ray, hit)) {			print("Hit "+hit.transform.name);		} else {			print("Hit nothing");		}	}
}
@Chesley suggested OnMouseDown, which may be a better solution in your case: when you must do always the same thing independently of the object clicked, use Raycast in a camera script; when you need each object to have a different reaction when clicked, use OnMouseDown *in the object's script*:
function OnMouseDown(){
  print(name+" was clicked!");
}

Viewing all articles
Browse latest Browse all 4

Latest Images

Trending Articles





Latest Images