Placing objects with a mouse click

Heey, i’m trying to make a tower defense game for school, and i found this script for placing objects in the scene.

var TowerPrefab : Transform;
var distance : int;

function OnMouseUp () 
{
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
	var hit : RaycastHit; 
	if (Physics.Raycast (ray, hit, distance)) 
	{ 
		Instantiate(TowerPrefab, hit.point, Quaternion.identity); 
}	

}

Now the problem is that this script just won’t work, i added the script to my camera, and added an object to TowerPrefab.
Please help.

For ray cast to work the object you are trying to press must have a collider attached to it. The script is fine.

the ground and the object both have colliders attached to it and it still won’t work :S

Instead of distance try using Mathf.Infinity. Im using similiar code in my current game but its just using iPhone input, here that is for reference:

Tile CheckRayCastHit()
	{	
		if( iPhoneInput.touchCount == 1 )
		{
			if( iPhoneInput.GetTouch(0).phase == iPhoneTouchPhase.Ended )
			{
				Ray pRay = Camera.main.ScreenPointToRay( iPhoneInput.GetTouch(0).position );
				RaycastHit pRaycastHit = new RaycastHit();

				// If we tapped on a tile
				if( Physics.Raycast( pRay, out pRaycastHit, kRayTraceDepth ) )
				{
					return (Tile) pRaycastHit.transform.gameObject.GetComponent( typeof( Tile ) );
				}
			}
		}
		
		return null;
	}

The problem doesn’t seem to be your code, its either the distance or the objects themselves.

Must the distance be exact or not?

No, its just telling the ray how far it should project before it stops.

Hm… I tried to get it to work as well, but had no luck what so ever. Would somebody mind taking the code and making an example scene with it working? (also it would be better if the object to be places was a variable so that it can be assigned via GUI).

Note: I just some Debug.Logs in, one after OnMouseDown and one in the if statement and neither were triggered.

Hope someone can get it to work since i need it for a school assignment

OnMouseUp isnt used to detect generic mouse ups. it kinda acts like its own raycast :slight_smile:

use a

if(Input.GetButtonUp(“Fire1”)) or another method of your choosing from within an Update call to detect when the user clicks.

the OnMouseUp function is literally a macro for doing a raycast against the object it is applied to. since you had it applied to your camera - you could never click on it because the camera probably doesnt have a collider :slight_smile:

Okay i know have this:

var TowerPrefab : Transform;
var distance : int;

if(Input.GetButton("Fire1"))
{
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
	var hit : RaycastHit; 
	if (Physics.Raycast (ray, hit, distance)) 
	{ 
		Instantiate(TowerPrefab, hit.point, Quaternion.identity); 
}	

}

And for one time it worked, but i could only place one object and when i pressed play again it didn’t work anymore HELP PLEASE

Okay so i just found out that when i edit the script that it works, for one time and then stops working again, anybody knows how to fix this?

var TowerPrefab : Transform;

if(Input.GetButton("Fire1"))
{
	var ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
	var hit : RaycastHit; 
	if (Physics.Raycast (ray, hit)) 
	{ 
		Instantiate(TowerPrefab, hit.point, Quaternion.identity); 
}	

}

Nope still won’t work, but thanks for helping

There is an open source inventory script that lets you drag items from inventory to the 3D world, I will see if I can’t find the code that places the object in the correct spot - You may even want to grab the whole script as well, as it could provide a good base for the tower selection interface.

That would be awesome :slight_smile:

When I did that script, I didn’t physically place anything, I used print(hit.point) to simply verify that I had a point there. I posted it simply on that reason. replace Instantiate(TowerPrefab, hit.point, Quaternion.identity); with print(hit.point); click and see if you get a message in the debug text. if not, then something else is wrong.

OH… also make sure that you are in function Update(){… code…}

var TowerPrefab : Transform;



function Update () {
	if(Input.GetButton("Fire1"))
	{
		var ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
		var hit : RaycastHit; 
		if (Physics.Raycast (ray, hit)) 
			Instantiate(TowerPrefab, hit.point, Quaternion.identity); 
	}
}

Yeaa that works, now only one problem left xD, when i try to just click once it stacks my towers, now is there anyway that when i click (even when i keep the mouse down) that there only appears one?

You need Input.GetButtonDown rather than GetButton for this.

Aaah didn’t even think of that, Thanks:)
And for anyone trying to build the same thing here’s the code:

var TowerPrefab : Transform;

function Update () {
	if(Input.GetButtonDown("Fire1"))
	{
		var ray = Camera.main.ScreenPointToRay (Input.mousePosition); 
		var hit : RaycastHit; 
		if (Physics.Raycast (ray, hit)) 
			Instantiate(TowerPrefab, hit.point, Quaternion.identity); 
	}
}

just attach the script to the camera and be sure your ground has a collider and the same for the object(tower) your placing

Peace :sunglasses:

can somebody convert it to c# pls? whenever I try to use the script I keep getting an error about hit not being assigned. (already converte to c# but i think i did it wrong for the hit variable)