selecting 3d objects fro mthe camera angle with the mouse

hi all,

I’m having a bit of trouble finding a way that allows me to select objects in game using the mouse.

The project im working on invloves basicly a large 3D map of a city that you move around useing the mouse (LMB draging to rotate and then clicking a button on the interface to switch modes to pan useing LMB drag) this is embeded into a website. if it helps [here](web map example map example.7z)(its a 7z file which is an archive, if you don’t have the extractor let me know and I’ll re-upload it in a different format) is a build of the map so far.

now in this city there are lots of buildings and i need to be able too click on these buildings (or a masking object in front of them) which then sends a message externally too the web page which displays information on the building. i already have the interactions between the player and the site worked out and tested so i just need a way to beable too click the buildings.

so yea thats my problem, any help would be much appreciated, if you need any more info about anything in the game to help solve it let me know

thanks,
hamish

I would send a Raycast from the camera through the mouse like the FPS tutorial and then you could find out what game object you collided with. Then you could get the information on the building. I think that answers your question.

I had to do something similar with a point and click game where if you clicked an enemy the character would fire at him. Here’s my code:

function Update () {
	var hit : RaycastHit;
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
	
	if(Input.GetButton("Fire1")) {
		Physics.Raycast (ray, hit, 100);

        //Important part below.
		if(hit.transform.gameObject.CompareTag("Enemy")) {
			// add  code here
		}
	}
}

The code finds out what the ray hit and if it matches the tag enemy then he attacked it. Obviously you will have to change a couple of details though.

If you add a GetComponent() call after finding the building to get a script with all its stats then access those numbers.

Hey peter,

thank you so much for your help i will give that a try and let you know how it goes.

EDIT: ok i actually do have one question, where do you add tags to objects?

Select the object and in the Inspector panel up top (under the name field) there’s a dropdown for ‘tags’ and ‘layers’.

To add a tag, click the tags dropdown, click ‘add new tag’, open the triangle and add a tag to the array.
Now you have to select it again and tag it.

-TT

thanks tornado,
ok im haveing abit of a problem with the script… i keep getting this error

NullReferenceException: Object reference not set to an instance of an object
building selection.Update ()   (at Assets\building selection.js:9)

my script at the moment is

function Update () {
   var hit : RaycastHit;
   var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
   
   if(Input.GetButton("Fire1")) {
      Physics.Raycast (ray, hit, 100);

        //Important part below.
      if(hit.transform.gameObject.CompareTag("t_operahouse")) {
	  print ("opera house clicked");
	  }
	        if(hit.transform.gameObject.CompareTag("t_gatehouse")) {
	  print ("gate house clicked");
         // add  code here
      }
   }
}

with the compare tag thing… i assume you put the name of the tag in there right?
dose it matter at all what object has this script in it?

here is a screenshot of how my object is set up… is the tag thing done right?

Hi hamsterhill, Do you find some solution to the problem?

I think your problem is that if the ray doesn’t hit anything, then the hit info will be null and your code is trying to access it, but it can’t so it throws a null reference exception.
So change this:

 Physics.Raycast (ray, hit, 100);

to this:

if(Physics.Raycast(ray, hit, 100)) {

and add the other other brace after this:

  if(hit.transform.gameObject.CompareTag("t_gatehouse")) { 
     print ("gate house clicked"); 
         // add  code here 
      }