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