- Home /
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!
Doug
Just so I give some constructive advice, I have made videos that demonstrates exactly what you want to know. I suggest using the Spherecast method if your cursor is locked and hidden, the Raycasting method if you are pointing to objects with the mouse :
some basic Raycasting information : http://www.youtube.com/watch?v=Sj4oxfQqzhA
And for destroying objects and incrementing a counter :
Raycast : http://www.youtube.com/watch?v=-XIaWwe$$anonymous$$wH4
Spherecast : http://www.youtube.com/watch?v=JaOsHCV_SZQ
Loius : mwah-hah-haaa >:D
@doyg can you TIC$$anonymous$$ ANY HELPFUL ANSWER to close out the question please. it's the round tick symbol on the left of answers
Answer by Julian_Spring · Apr 05, 2013 at 06:51 AM
There are lots of different ways to do this :)
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
if(Input.GetKeyDown(KeyCode.E)){
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 :)
Thanks a bunch mate :) I ran it in the cursor script, but i got an error saying unknown identifier: "gameobject" any tips?
Thanks again, the help is really appreciated!
oh wait, sorry, that was me being an idiot, i needed to change gameOject to gameObject XD
Haha, sorry about that. Very glad that I was helpful :P
Your answer
Follow this Question
Related Questions
Object Interaction 1 Answer
Getting GameObject from a grid (array) using raycasters 0 Answers
How to detect if a raycast ray stop hitting an object 1 Answer
Gameobject follow cursor 2D 0 Answers
Raycasting an empty object 1 Answer