Pick up game objects with cursor (raycasting)

I’m a total noob with code, and I was wondering how to use the cursor to interact (pick up/play) with in game objects? Any help would be amazing! Thanks!


There are lots of different ways to do this :slight_smile:

So I’ll start for you so you can take it from there.

var hitObject : GameObject;
var Range : float = 1;

function Update (){

var fwd = transform.TransformDirection (Vector3.forward);
var hit : RaycastHit;
	if(Physics.Raycast (transform.position, fwd, hit, Range)){
		Debug.DrawRay(transform.position, fwd * Range, Color.red);    
			hitObject = hit.collider.gameObject

						hitObject.transform.parent = gameOject.transfom;
						return true;




I don’t know if this will work not tested but I hope you can take it from there.
As I wrote this in notepad :slight_smile: