RotateAround Limitations

Hey Unitarians!

In my game, I have a block called a swing block. The block part of it attaches to the ceiling of the game and the cord that is attached to it rotates constantly. It moves back and forth like a never-ending pendulum. At least, that’s what it should do.

What I have right now is that the cord of the swing rotates around the swing block, as it should, but it keeps going all 360 degrees - making a circle. This is probably because it is in the update function. I want it so that after 30 degrees of moving to the left, it switches direction and moves to the right, until it reaches 30 degrees in that direction, and switches back. How would I do this?

Currently my script:
using UnityEngine;
using System.Collections;

public class SwingBlock : MonoBehaviour {

	public float RotateSpeed = 100;
	public float angleMax = 30.0f;

	private Transform SwingRoot;
	//private Transform EndRoot;

	void Start () {
		SwingRoot = this.transform.parent.gameObject.transform;
	void Update () {
		//Rotation controller
		transform.RotateAround (SwingRoot.position, Vector3.forward, RotateSpeed * Time.deltaTime);

I think the best way to go is to use a Hinge Joint.
Some examples: EX1, EX2.

If that is not possible, for any reason, you’ll have to script your limits. Take a look here.

When your pendulum gets to the proper angles, use -1.0f * Vector3.forward as the rotation angle. Store the original Vector3 and determine the present angle . Something like this.

void Start()
    Vector3 origAngle = SwingRoot.position;

void Update()
    float curAngle = Vector3.angle(SwingRoot.position, origAngle);

    Vector3 rotAngle;
    if(curAngle >= angleMax)
        rotAngle = -1.0f * Vector3.forward;
        rotAngle = Vector3.forward;

    transform.RotateAround (SwingRoot.position, rotAngle, RotateSpeed * Time.deltaTime);