• 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 ICHeeryI · Jan 11, 2014 at 06:24 PM · other

Drag door, rotating based on 2 vectors?

Hi there, I need realistic opening and closing door based on mouse movement. I already tried shadows demo project. And I tried writing my own script(I wanted to fix few bugs) but I gave up, now I really need some help. This is what I have so far:

  1. Mouse is over door > We clicked > Mouse position is stored as Vector3

  2. Now we can drag > Each frame gets new mouse position > Storing in new Vector3 variable

What now?! How can I rotate object based on 2 vectors(Mouse position on door and in 3D space). For example:alt text

I could use hinge joint or something like that to do that green part on image. But then for rotating I would need to use force? But how to do that based on 2 vectors?! Thanks for reading.

untitled.jpg (25.9 kB)
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 Zephire · Jan 11, 2014 at 07:45 PM 0
Share

$$anonymous$$eeping it on a hinge joint seems like the sensible thing to do. Would it be an idea to increase/decrease the force used to open it when clicked based on the upwards or downwards movement of the mouse?

avatar image ICHeeryI · Jan 11, 2014 at 07:56 PM 0
Share

But how to use that force? I mean how to apply(addforce). And its good idea, just I don't know how to realise.

avatar image ICHeeryI · Jan 12, 2014 at 09:02 AM 0
Share

Bump :/...I guess I will try posting this on forum

2 Replies

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

Answer by robertbu · Jan 12, 2014 at 11:14 AM

There are a few thorny issues with problem. The primary one is how to map 2D mouse movements into 3D movement. And that mapping will depend on which side the hinge is on (or what side of the door you are viewing). You could use a hinge joint and a force model to open/close the door, but given your description, I don't think you buy much with implementing it this way.

Here is an alternate solution specific to the door opening you have in your diagram: hinge on the right, opening in. This code assumes that you have a door with a pivot point in the center, and a parent empty game object placed on the left edge of the door as the hinge. The script goes on the child, visible object. If you know how to use a 3D modeling problem, you can create a door with the pivot point on the edge and eliminate all the 'parent' references in the script. Note it intreprets mouse down or mouse left movements as closing and mouse up or mouse right movements as opening.

 #pragma strict
 
 var factor = 1.0;
 var minRot = 0.0;
 var maxRot = 90.0;
 
 private var startPos : Vector3;
 private var startRot : float;
 private var currRot : float;
 
 function Start() {
     startRot = transform.eulerAngles.y;
 }
 
 function OnMouseDown() {
         startPos = Input.mousePosition;
 
 }
     
 function OnMouseDrag() {
         var delta = Input.mousePosition - startPos;
         currRot = startRot + delta.x * factor + delta.y * factor;
         currRot = Mathf.Clamp(currRot, minRot, maxRot);
         transform.parent.eulerAngles = Vector3(0.0, currRot, 0.0);
 }
 
 function OnMouseUp() {
     startRot = currRot;
 }

A couple of caviats. First, this code is not platform independent since it uses Screen coordinates to calculate the angle. If you are aiming for multiple platforms, you will have to do something to modify 'factor' for individual platforms. Second the use of OnMouseDown() and OnMouseDrag() is expensive when considering mobile.

Comment
Add comment · Show 2 · 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 ICHeeryI · Jan 12, 2014 at 11:32 AM 0
Share

Thanks for great idea and code! Its working really good. It have few bugs, I think I will be able to fix it.

avatar image robertbu · Jan 12, 2014 at 04:39 PM 0
Share

If you let me know what bugs you found, I'll take a look. It worked for me without problems given the constraints I outlined.

avatar image
0

Answer by Toggy11 · Jan 12, 2014 at 11:09 AM

Create an animation. Then, detect the force of the mouse and put the animation at that point.

Example the force is 20. Animation goes across 2 seconds.

Comment
Add comment · Show 1 · 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 ICHeeryI · Jan 12, 2014 at 11:34 AM 0
Share

That's not the best way. Anyway thanks for any help.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Read string from other script (C#) 1 Answer

How do I call a variable from ScriptA into a function in scriptB? 1 Answer

Create blips relative to the players position on a minimap. 1 Answer

Access guiText from other object. 3 Answers

Why i can not access these variables? 1 Answer


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