- Home /
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
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;
}
}
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..
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.
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
I took a look at your package. In order to get the code above to work:
Disable your Pendu script on G$$anonymous$$
Add the script above to bottomG$$anonymous$$
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.
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.
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
Follow this Question
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