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 navadeep2011 · Mar 12, 2013 at 09:47 AM · rotationphysicsmouse-drag

How will we give from a fixed point to rotate operation by using mouse?

Hi All,

I want to make simple pendulum. I want like that http://phet.colorado.edu/sims/pendulum-lab/pendulum-lab_en.html in the above link by using mouse we can drag that pendulum a side.

But in mouse drag my code is this

 using UnityEngine;
 using System.Collections;
 [RequireComponent(typeof(MeshCollider))]
  
 public class DragScript : MonoBehaviour 
 {
  
 private Vector3 screenPoint;
 private Vector3 offset;
 void OnMouseDown()
 {
     screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
     offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
 }
 void OnMouseDrag()
         
 {
     Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
 Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
 transform.position = curPosition;
 }
 }

by using this total position is changed. but i want only rotation. i also tried

 function Update () {
 var mousePos = Input.mousePosition;
 mousePos.z = 10.0f; //The distance from the camera to the player object
 var lookPos : Vector3 = Camera.main.ScreenToWorldPoint(mousePos);
 lookPos = lookPos - transform.position;
 var angle : float = Mathf.Atan2(lookPos.y, lookPos.x) * Mathf.Rad2Deg;
 transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
 }

that is calculate mouse position and then rotate at finally my question is how will i do that rotation by using mouse?

Thanks to all

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
1

Answer by robertbu · Mar 12, 2013 at 12:06 PM

One way is to build up the pendulum out of three game objects with a parent/child relationships. Start with a pivot game object. It can be a real game object, or an empty game object, but it is the point that everything will rotate around. Then make an arm, position it and make it a child of the pivot. Then make a bob/weight, position it and make it a child of the arm. The following source attaches to the bob/weight. You will need to drag the pivot onto the 'pivot' variable.

using UnityEngine; using System.Collections;

 public class DragRotate : MonoBehaviour {
     
     public Transform pivot;
     private float angle;
     private Vector3 v3Pivot;  // Pivot in screen space
 
     void Start () {
         v3Pivot = Camera.main.WorldToScreenPoint (pivot.position);
      }
     
     void OnMouseDown() {
         Vector3 v3T = (Vector3)Input.mousePosition - v3Pivot;
         angle = Mathf.Atan2 (v3T.y, v3T.x) * Mathf.Rad2Deg;
     }
     
     void OnMouseDrag() {
         Vector3 v3T = (Vector3)Input.mousePosition - v3Pivot;
         float angleT  = Mathf.Atan2 (v3T.y, v3T.x)  * Mathf.Rad2Deg;
         float angleDiff = Mathf.DeltaAngle(angle, angleT);
         pivot.Rotate(new Vector3(0.0f, 0.0f, angleDiff));
         angle = angleT;
     }
 }
Comment
Add comment · Show 5 · 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 navadeep2011 · Mar 12, 2013 at 04:25 PM 0
Share

Hey $$anonymous$$r.. Robertbu .. thanks for answering but that code didn't work for pendulum. shankarraopilli@gmail.com and my skype id is : shankar.clite if u give ur mail id than i will send my unity simplependulum package. please help me..

avatar image robertbu · Mar 12, 2013 at 05:14 PM 0
Share

I just answer questions on the list. If you want to provide a link to a zip file on this list, I'll take a look to see what is going on. But I tested the script above before I posted the code, and it works fine. You need to construct the games objects as I've described for it to work.

avatar image navadeep2011 · Mar 13, 2013 at 02:45 AM 0
Share

Yes.. $$anonymous$$r Robertbu.. please find this link and tell me.. https://dl.dropbox.com/u/71403050/PendulumExp.unitypackage by default it is rotation.. but i want only drag rotate of pendulum

avatar image robertbu · Mar 13, 2013 at 03:26 AM 0
Share

I took a look at your package. In order to get the code above to work:

  1. Disable your Pendu script on G$$anonymous$$

  2. Add the script above to bottomG$$anonymous$$

  3. Add a collider to bottomG$$anonymous$$. I added a capsule collider. On$$anonymous$$ouseDown() requires the object have a collider, which is probably why you did not get the code to work.

  4. Select bottomG$$anonymous$$ and drag G$$anonymous$$ to the Pivot variable

That will solve the dragging issue, but I expect there will be significant work in connecting this script to the Pendu script.

Another issue. The motion you've created for the pendulum is not sinusoidal. Go out to this post and grab the script written by me half way down the page:

http://answers.unity3d.com/questions/392492/pendulum-with-a-moving-pivot.html

Start with an empty scene and follow the directions in the post. Compare the feel of the motion by this script with the motion of your script. This script is not what you want to base your code on, but it gives you a target for the feel of the motion.

avatar image navadeep2011 · Mar 13, 2013 at 04:54 AM 0
Share

Yes.. Robertbu your absolutely correct. And also thanks for helping me.. For motion i applied ur code that is fine. and also looks very good. but i want to pass drag angle to stripe Gm. that depended on that angle i want motion. in your script by default u gave angle. Thankyou so much

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

11 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

Related Questions

Rotating a Rigidbody with Physics 1 Answer

Simulating the graphics of a rolling 3D ball in a 2D game 2 Answers

Mouse rotation with limits: limit overrun protection works on positive overruns only? 0 Answers

How may I observe expected physical interactions while using Rigidbody.MoveRotation()? 1 Answer

Keep rotation when changing parent in Unity 3D 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