Motion script for simple pendulum?

Hi All,

I want make this simple pendulum Pendulum Lab 2.03 please check this.
My DragRotate script is working fine… But i want to give motion depending on that motion drag angle.

By default i will give angle but i want to give some thing like this in the link.
my drag rotate code is like this

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;
}

}

and my motion code is like this

using UnityEngine;
using System.Collections;
public class Pend : MonoBehaviour {
private float angle = 0.0f;
private float speed = 1.5f;

Quaternion qStart, qEnd;

void Start () {
   qStart = Quaternion.AngleAxis ( angle, Vector3.forward);
   qEnd   = Quaternion.AngleAxis (-angle, Vector3.forward);


}

void Update () 
{
   angle -= Time.deltaTime;
   if (angle < 0)
     angle = 0;
    qStart = Quaternion.AngleAxis ( angle, Vector3.forward);
    qEnd   = Quaternion.AngleAxis (-angle, Vector3.forward);
    transform.rotation = Quaternion.Lerp (qStart, qEnd, (Mathf.Sin(Time.time * speed) + 1.0f) / 2.0f);
}

}

but i want to get that angle than due to that i will give motion … please help me.

Thanks to all.

When posting code by others, you need to make reference to the original post or website. Reasons:

  1. When you post code (especially when you say something like, “my motion code”), it is implicitly assumed that you both wrote and understand the code you posted. This is not fair to the original authors.
  2. Original posts contain information that often helps inform the solution.
  3. People answering your questions make assumptions about what you know based on the code you post. So the answers are often a mismatch to the level of the programmer when they are asking for help to modify “borrowed” code.

With that said, here is a merge of the two scripts. For it to work, the arm has to be pointed right (angle 0) when there is 0 rotation on the ‘Z’ axis:

using UnityEngine;
using System.Collections;

public class Pendulum : MonoBehaviour {
	
	public Transform pivot;
	public float    speed = 0.5f;
	private Vector3 v3Pivot;  // Pivot in screen space
	private bool    dragging = false;
	private float   startAngle = 0.0f;
	private float   endAngle = -180.0f;
	private float   fTimer = 0.0f;
	private float   angleDiff;
	private Vector3 v3T = Vector3.zero; 
	private float   angle;

	void Start () {
		v3Pivot = Camera.main.WorldToScreenPoint (pivot.position);
	 }
	
	void Update() {
		if (!dragging) {
			float f = (Mathf.Sin (fTimer * speed - Mathf.PI / 2.0f) + 1.0f) / 2.0f ;
			
			v3T.Set (0.0f, 0.0f, Mathf.Lerp(startAngle, endAngle, f));
			pivot.eulerAngles = v3T;
			fTimer += Time.deltaTime;
		}
	}
	
	void OnMouseDown() {
		dragging = true;
		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;
		angleDiff = Mathf.DeltaAngle(angle, angleT);
		pivot.Rotate(new Vector3(0.0f, 0.0f, angleDiff));
		angle = angleT;
	}
	
	void OnMouseUp() {
		
		Vector3 v3T = Camera.main.WorldToScreenPoint(transform.position); 
		v3T = v3T - v3Pivot;
		float angle = Mathf.Atan2 (v3T.y, v3T.x) * Mathf.Rad2Deg;
		
		Debug.Log (angle);
		
		if (angle < 0.0f) 
			angle += 360.0f;
		
		startAngle = angle;
		
		if (angle <= 90.0f) {
			endAngle = angle - 2.0f * angle - 180.0f;
		}
		else if (angle <= 180.0f) {
			endAngle = angle + 2.0f * (180.0f - angle) + 180.0f;
		}
		else if (angle <= 270.0f) {
			endAngle = angle + 2.0f * (270.0f - angle);
		}
		else {
			endAngle = angle - 2.0f * (angle - 270.0f);
		}
		
		dragging = false;
		fTimer = 0.0f;
	}
}

I created a tutorial with the Euler-Cromer method to simulate a simple pendulum.