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 Goldenvale · Mar 11, 2015 at 04:29 PM · objectmousedragpickup

Dragged object keeping it's original rotation.

I'm trying to make the character pick up and drag an object, when he rotates the object it should keep facing the character/camera while keeping it's original rotation. I've already tried Transform.LookAt but that makes the object's forward vector face the camera.

Here's my code (Drag() decides the final position):

 var grabbed : Transform;
       var grabDistance : float = 10.0f;
       var grabLayerMask : int;
       var grabOffset : Vector3; //delta between transform transform position and hit point
       var useToggleDrag : boolean; // Didn't know which style you prefer. 
       
       function Update () {
           if (useToggleDrag){
               UpdateToggleDrag();
           } else {
               UpdateHoldDrag();
           }
       }
       
       // Toggles drag with mouse click
       function UpdateToggleDrag () {
           if (Input.GetMouseButtonDown(0)){ 
               Grab();
           } else {
               if (grabbed) {
                   Drag();
               }
           }
       }
       
       // Drags when user holds down button
       function UpdateHoldDrag () {
           if (Input.GetMouseButton(0)) {
               if (grabbed){
                   Drag();
               } else { 
                   Grab();
               }
           } else {
               if(grabbed){
                   //restore the original layermask
                   grabbed.gameObject.layer = grabLayerMask;
               }
               grabbed = null;
           }
       }
       
       function Grab() {
           if (grabbed){ 
              grabbed = null;
           } else {
              var hit : RaycastHit;
              var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
              if (Physics.Raycast(ray, hit)){  
                  if(hit.collider.gameObject.GetComponent.<Rigidbody>() != null) {   
                      grabbed = hit.transform;
                      if(grabbed.parent){
                          grabbed = grabbed.parent.transform;
                      }
      
                      //set the object to ignore raycasts
                      grabLayerMask = grabbed.gameObject.layer;
                      grabbed.gameObject.layer = 2;
                      //now immediately do another raycast to calculate the offset
                      if (Physics.Raycast(ray, hit)){
                          grabOffset = grabbed.position - hit.point;
                      } else {
                       //important - clear the gab if there is nothing
                       //behind the object to drag against
                          grabbed = null;
                      }
                  }
              }  
           }
       }
       
       function Drag() {    
           var hit : RaycastHit;
           var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
           if (Physics.Raycast(ray, hit)){      
               grabbed.position = hit.point + grabOffset;
           }
       
       }
Comment
Add comment · Show 13
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 AlwaysSunny · Mar 11, 2015 at 04:24 PM 0
Share

"and when he rotates the object should keep facing the character/camera while keeping it's original rotation"

I think I understand what you mean, but your description is erroneous if so. Say I pick up a globe with Africa facing me, and turn myself around 180 degrees. Africa is still facing me, but the globe has turned, just with me. I assume this is what you want, correct?

avatar image Goldenvale · Mar 11, 2015 at 04:43 PM 0
Share

Sorry for the typo. Yes, this is exactly what I want. And I already tried what you suggested, it didn't work.

avatar image AlwaysSunny · Mar 11, 2015 at 06:08 PM 0
Share

I'm not convinced I grasp what you're doing (you're dragging only "against" the surface immediately behind the dragged object?). Are you trying to tack your object to the environment?

Could you have another go at explaining your scenario? Screenshots, diagrams, or videos can be helpful.

avatar image Goldenvale · Mar 11, 2015 at 06:25 PM 0
Share

When I pick an object up from any angle, it has to rotate with me when I turn the camera. That's it.

Here's a screenshot:

alt text

avatar image AlwaysSunny · Mar 11, 2015 at 06:56 PM 1
Share

So when it does interact, should rotation be allowed? In other words, if you bump this dragged object against another body, the physically accurate thing to do would be to cease enforcing the tracking behavior you're implementing and allow the simulated rotation. Either reacquire the new tracking solution when the collision ceases, or "snap back to" the original tracking. (I've seen both behaviors in games before).

Never$$anonymous$$d that enforcing the position of a non-kinematic rigidbody can have disasterous consequences on the purity of the physics simulation. You could easily create a scenario where dragging an object causes others to skyrocket into the distance. This is not as trivial as it might appear.

In most FPS situations where you see this, I'd wager a grabbed item is parented to the player to achieve the looking-at-Africa effect you're seeking. I get why you can't do this, because you're trying to simultaneously place the grabbed object away from the player at whatever location you're picking with the cursor or crosshair. Bit tricky.

In any event, you're wanting to maintain the specific tracking you had when the drag operation started, so you need to deduce and maintain the original orientation between the object and player... Hmm.

If it were me, I'd try this:
Create an empty helper object. This object will LookAt the camera, and reside at the end of your raycast (where you want the dragged object's position). When your drag operation begins, place the helper object at the point where you hit the dragged object and parent the dragged object to the helper. (Parenting and rigidbodies normally do NOT play nice, but you're already futzing with the physis sim in a dangerous way, so anything goes). Un-parent when the operation ends.

Give this a try; if nothing else, maybe it'll shed some light on what you can and can't do in this situation and give you some fresh ideas.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Bullet mouse control 2 Answers

How can I manipulate an object with the mouse? 1 Answer

OnMouseUp() Click Display Effect. 1 Answer

Dragging movement Speed 1 Answer

How Can I Drag the Object With Touch ? (Mobile) 4 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