Instantiate a prefab on touch position Raycast

Hi All,

After a long search I found some code to get a prefab on the same location I touch and it works.
My Question is, is there a way to get the prefab instantiate a few units above the point i touch?
The prefab is a cube with a rigid body so they fall at the point I touch.

Thank in advance!

public var newObject : GameObject;

var layerMask = 1 << 8;

function Update () {
	for (var Touch : Touch in Input.touches) {
		if (Touch.phase == TouchPhase.Began) {
			
			var ray = Camera.main.ScreenPointToRay (Touch.position);
			var hit: RaycastHit;
			if (Physics.Raycast (ray, hit, layerMask)) {
				Instantiate (newObject, hit.point, transform.rotation);
			}
		}
	}
}

you just need to increase the y value when you instantiate.

new Vector3(hit.point.x, hit.point.y + 10, hit.point)

Hi handsomePATT,

Thanks for your advice!

I tried to put you code in my instantiate line but getting errors, i’m sure it’s me that is doing it wrong :wink:
I have very very limited programming know how.
Can you please show me how to put you code in my script?

Thanks a lot!

probably cuz mine is c#. i think javascript is the same just take out the new keyword.

Still not working in my case when I click the object some times instantiated in the right place and some times is not, not really stable!!! any one who has tried it?

TE AMO BOLUDO, ESTOY HACE 4 HORAS XAShdgclkfaj