- Home /
How to Parent object to camera OnMouseDown? (javascript)
Hey again.
What I'd like to do here is include a function onmousedown which will parent the object the script is attached to, to the camera. The end effect would be holding the object ala Half Life 2 or other Source games.
As always I'm here to learn, so please don't just include a script; help me understand why it works.
Thanks everyone!
Answer by Joshua · Apr 30, 2011 at 01:05 AM
I'd love to explain the code, but your question is so extremely straightforward that it's hard to think of anything that would need further explanation. Just have an if() in your update function that checks if the left mouse button is down, and in case it is - parent the object to the camera.
function Update () {
if(Input.GetMouseButtonDown(0)) {
transform.parent = Camera.main;
}
}
If you are not using the main camera you could declare a camera variable at the top (var cam : Camera) and then through the inspector drag 'n drop the camera into the slot.
But I don't think this is exactly what you want, because attaching this to EVERY object you want to apply this to would be impracticable and slow down your system if you have a lot of them. What I would do is when you get the mouse down cast a ray forward from your mouse into your scene, see if it hit something, check if that object is tagged "whatever" and then parent it. This way you would be able to click on an object to have it follow the camera.
var inHandPosition : Vector3; //where is it when 'in your hand' var endPosition : Vector3; //where is it when it's on the grill
var objectPrefab : GameObject; //prefab of the to be inst. obj.
var cam : Camera = Camera.main; //change this if you don't use the main cam
private var spawnObject : GameObject;
function Update () {
if(Input.GetMouseButtonDown(0)) { //did you click?
var ray : Ray; //the ray you will be casting
ray = cam.ScreenPointToRay(Vector3(Camera.main.pixelWidth*cursor.transform.position.x,Camera.main.pixelHeight*cursor.transform.position.y,0));
//this bit is a little confusing pixelWidth/Height is how many pixels large the screen is,
//cursor.transform.position.x/y is at what percentage of width/height the mouse is,
//so multiply to get the x/y pixelposition of your cursor, which is where we want the ray to start
//ScreenPointToRay automatically makes the ray go towards the direction you are facing with your camera from the pixel position
var hit : RaycastHit //that which will be hit (if any)
if(Physics.Raycast (ray.origin,ray.direction, hit)) { //true if there is a hit, else false
if(hit.collider.gameObject.tag == "tag you gave the start object") { //if the object you clicked on is the one you wanted..
spawnObject = Instantiate(objectPrefab,inHandPosition,Quaternion.Identity);
spawnObject.transform.parent = cam.transform;
}
else if(hit.collider.gameObject.tag == "tag you gave the end object" && spawnObject.transform.parent == cam.transform) { //if you have the object in-hand and you click on the target
spawnObject.transform.parent = hit.collider.gameObject;
spawnObject.transform.position = endPosition;
}
}
}
}
There you go. Enjoy!
Would you care to explain the ray casting? In fact, I'd prefer to do it that way, but its so ridiculously complex to me as a near-beginner that I don't fully understand how it works. Thanks for your answer though!
If it helps, what I'm ai$$anonymous$$g for here is a short-order cook type game. So this script is intended to allow me to pick objects up and then inevitably parent them to something else (of another tag) in order to put them down to cook, etc.
No problem, I know how you feel - two months ago I had never written a single piece of code in my life ^.^ just make heavy use of the docs (http://unity3d.com/support/documentation/ScriptReference) and this site.
You'll have to explain in detail what you want to happen though - because if I'm going to write a longer script it's very frustrating if it's all for nothing. Here's a guess from the context:
You click an on object with tag "a". Object follows camera (maybe the object should fly towards the camera first, so it wont collide when you move the cam?). You click on an other object with
with tag "b" (or is it also a?) and the first object becomes parented to the second one?
Thats exactly it.
Here is the order of operations I'm looking for:
[1.] I click on a box of hamburgers. (named "TastyBurgers") [2.]$$anonymous$$y hamburger patty (named "beefpatty_raw" with the tag "grill") is instantiated and placed "in my hand". [3.]I click on the grill surface (tagged "grillSurface"), and it is parented to that.
---Thanks again for all the help.
You are the man. Thank you very very much. Great commenting too, I really appreciate it.
Answer by kjw1998 · Nov 15, 2011 at 10:30 PM
you know for the top simple code it says it cannot convert the main camera to transform