Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by LukaKotar · May 08, 2012 at 01:59 PM · rotateaxisdoorinput.getaxisopen

Rotate the door according to mouse movement

I am developing a first person game similar to "Amnesia: The Dark Descend". I added a collider to the door that is twice as wide as the door and it's original collider, and added a script to ignore the wider one. That way it rotates around where the hinge should be. Now I am working on a script to rotate it in the game, so that when the user holds down the "Fire1" button and moves the mouse around ( Input.GetAxis("Mouse X") ), the door rotates around it's Y axis.

This is what I am trying to achieve.

Thanks for your attention.

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

1 Reply

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

Answer by Berenger · May 08, 2012 at 02:12 PM

Very interesting. I thought about it and here is what I came with :

The process has 3 steps and the final result will be the vector direction to rotate the door , from the rotation axis to the extremity of the door.

  • First, the initial click. You need to find the position of the click on that door and you need to store it. Use raycast and raycasthit.

  • Secondly, you need to find the the vector [InitialPos, CurrentPos] of the mouse on the horiztonal plane defined by the normal (0,1,0) and the point from the first step. Then you can use Plane.Raycast, the position being calculated like that : cam_pos + from_cam_to_mouse.normalized * dist.

  • Finally, the result vector will be that point minus the position of the rotation axis (with y == 0).

Now, you can use Quaternion.LookRotation and Vector3.Lerp to rotate your door.

PS : Now idea if it works, just what came through my mind.

Comment
Add comment · Show 14 · 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 LukaKotar · May 08, 2012 at 02:16 PM 0
Share

Thanks! I'll try it right away! I'll tell you if it works. ;)

avatar image LukaKotar · May 08, 2012 at 02:44 PM 0
Share

I am wondering how the second part is done. :/ Can you please give me an example? :)

avatar image Berenger · May 08, 2012 at 02:54 PM 0
Share

Sure. C# though, but javascript is quite close.

// Called when the user clicked the door and is holding the mouse down. private Vector3 PlanarPos( Vector3 clickPos ) { // Creation of the plain from the clickPos Plane P = new Plane( Vector.up, clickPos );

 Vector3 mousePos = Input.mousePosition;
 mousePos.y = Screen.height - mousePos.y; // ScreenPointToRay is using (0,0) as the bottom left corner.
 mousePos.z = 0f; // correction from lyonlet

 Ray ray = Camera.main.ScreenPointToRay(mousePos);
 float d;
 if( P.Raycast( ray, out d) )
 {
     return Camera.main.transform.position + ray.direction.normalized * d;
 }
 
 Debug.LogError( "Error, something went wrong during the raycast" );
 return Vector3.zero;

}

avatar image LukaKotar · May 08, 2012 at 02:59 PM 0
Share

Thanks! :)

avatar image LukaKotar · May 08, 2012 at 03:08 PM 0
Share

Assets/Scripts/DoorScript1.cs(15,27): error CS1501: No overload for method ScreenPointToRay' takes 3' arguments

Show more comments

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

6 People are following this question.

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

Related Questions

UnityException: Input Axis Rotate is not setup. 2 Answers

Rotate object on the Y axis 90 degrees every 5 minutes? 1 Answer

Open door with key 2 Answers

Press an in-game switch to open a door - help? 2 Answers

Open Door Once But Sound Plays again when i trigger at the door 3 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