Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Brandon 9 · Apr 30, 2011 at 12:23 AM · cameraparentgrab

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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!

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Brandon 9 · Apr 30, 2011 at 01:08 AM 0
Share

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.

avatar image Joshua · Apr 30, 2011 at 01:13 AM 0
Share

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

avatar image Joshua · Apr 30, 2011 at 01:13 AM 0
Share

with tag "b" (or is it also a?) and the first object becomes parented to the second one?

avatar image Brandon 9 · Apr 30, 2011 at 01:19 AM 0
Share

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.

avatar image Brandon 9 · Apr 30, 2011 at 01:43 AM 0
Share

You are the man. Thank you very very much. Great commenting too, I really appreciate it.

Show more comments
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

First Person Object grab gets off center when colliding? 0 Answers

RTS Camera problem. 0 Answers

parent object rotate with child camera 1 Answer

rigidbody on parent has to effect the child players but not the child camera's attatched to the players 0 Answers

Objects parented to "FPS-Camera" goes through terrain and walls! -1 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges