Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Catlard · Aug 23, 2011 at 11:37 AM · rigidbodyraycastingdragdrop

Drag and drop ray-casting conundrum!

Howdy, folks.

So, I'm attempting get my player object to be drag and droppable via the mouse. I'm doing this by allowing the object to translate based on a certain ratio between the mouse's position on screen and the 3d space. When I run the script below it begins to work...but then crashes with a NullReferenceException error pointing to the line that translates the object in the script. It crashes when I move only a little ways away from the object's origin...but until then it appears to work fine. Could it be because the object I'm trying to "drag" around has a rigidbody on it, and rigidbodies don't like translation? I'm really kind of miffed as to why this is happening. If you can figure out this spooky mystery, I will proudly present you with the key to the city in front of a crowd of cheering citizens...

Thanks for your help, in advance!

--Simon

 var pickedUpPlayer = false;

var originalMouseX = 0.000; var originalMouseY = 0.000; var deltaMouseX = 0.000; var deltaMouseY = 0.000; var screenRatio = 100.000;

function FixedUpdate () { var hitOrigin : RaycastHit; var rayOrigin = Camera.main.ScreenPointToRay (Input.mousePosition);

 if ( Physics.Raycast( rayOrigin, hitOrigin, 100 ) && !pickedUpPlayer)
 {
     //detects if the player has been clicked on.
     if (Input.GetMouseButton(0))
     {
         //if the user has clicked on the player tagged object, they can start to move him or her around via the next if statement...
         //so we need to know at what point on the screen the player has clicked, such that in the next if statement we can determine
         //the change in mouse position, and translate the player's transform accordingly.
         if ( hitOrigin.collider.gameObject.renderer && hitOrigin.collider.gameObject.tag == "Player")
         {
             pickedUpPlayer = true;
             originalMouseX = Input.mousePosition.x;
             originalMouseY = Input.mousePosition.y;
         }
     }
 }
 //allows you to drag the player according to where the mouse is on the screen. The game knows that the distance the mouse moves onscreen
 //coordinates to a certain distance that it is moving in the 3d space. since there is no z coordinate changed, this works.
 if (pickedUpPlayer)
 {
     deltaMouseX = Mathf.Abs(Input.mousePosition.x) - Mathf.Abs(originalMouseX);
     deltaMouseY = Mathf.Abs(Input.mousePosition.y) - Mathf.Abs(originalMouseY);
     print("Delta X: " + deltaMouseX + ", Delta Y: " + deltaMouseY);
     hitOrigin.collider.gameObject.transform.Translate(deltaMouseX / screenRatio, deltaMouseY / screenRatio, 0);
 }
 if (!Input.GetMouseButton(0) && pickedUpPlayer)
 {
     pickedUpPlayer = false;
     print("Dropped the player");
 }

}

Comment
Add comment · Show 3
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 Waz · Aug 23, 2011 at 11:43 AM 0
Share

Format the code using the 101-010 button.

avatar image Waz · Aug 23, 2011 at 11:44 AM 0
Share

Order expressions such that cheapest is left of &&, not right of, as it short-circuits.

avatar image Waz · Aug 23, 2011 at 11:56 AM 0
Share

Which city are you mayor of exactly?

1 Reply

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

Answer by Waz · Aug 23, 2011 at 11:52 AM

Your code relies on the ray keeping a bead on the player collider, otherwise it will crash. Currently it is ray casting every frame even after you've picked up the player, and that can't be right. It then keeps using that hit to try to keep knowing what to translate, but it could be anything, or nothing in subsequent frames. You're also translating it by an amount that keeps increasing.

Basically, read the code out aloud to yourself in high-level terms and you'll see it doesn't really implement a logical attack on the task.

Keep a script variable that is the object being dragged:

 var dragged : Transform;

Start from there and you should make it.

Comment
Add comment · Show 3 · 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 Catlard · Aug 23, 2011 at 01:48 PM 0
Share

Thanks for looking it over, Warwick. I understand what you're saying, and I'll try again...I guess I thought that it wouldn't matter if the ray was still hitting the player, as long as the...OH, DUH. Yeah, thanks. I'll see what I can get up to in the next few $$anonymous$$utes. To answer your questions:

1) I always try pushing the format code button, but it seems to never format everything correctly. $$anonymous$$aybe it's some formatting thing I'm doing in my code that's disrupting it? Not sure.

2) Good to know about short-circuiting. I had always wondered if that's the way it worked.

3) Um...well...at the moment, I am a mayor of the totally lame city of Phrenology-town. I assume you don't ever go there intentionally (if at all :), but the next time you're in, I'll have the key sent up to your hotel room.

avatar image Catlard · Aug 23, 2011 at 01:55 PM 0
Share

Ah, yes, so I've fixed the crashing problem. Now I just have to fix the cumulative speed issue. Thanks for your clear assessment and giving me an enticing hint on how to do it myself.

Cheers.

S

avatar image Waz · Aug 23, 2011 at 09:26 PM 0
Share

Feel free to edit your question with your new code. Otherwise, my tip would be to not try to use a delta but rather a new absolute position. Check the docs of ScreenToWorldPoint.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Pick up, drop, rotate, and zoom a rigidbody gameObject 2 Answers

How do I mimic dragging and dropping a FBX to the hierarchy window with an editor script? C# 1 Answer

Collision objects when I drag and drop them by touch (Unity Android) 0 Answers

How to Stop a Game Object from Passing through a Collider while it's being Dragged? 3 Answers

Drag Rigidbody 2 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