- Home /
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);
}
}
Answer by Tehnique · Jul 20, 2014 at 03:48 PM
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.
I'm not really getting the Hinge Joint, and sicne my game is made for mobile, I think if I have a lot of them they might affect performance. I'll try scripting the limits
Answer by AndyMartin458 · Jul 21, 2014 at 05:12 PM
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;
}
else
{
rotAngle = Vector3.forward;
}
transform.RotateAround (SwingRoot.position, rotAngle, RotateSpeed * Time.deltaTime);
}
I'm trying your script, but I'm not seeing anything different. I modified one line of your code where it said Vector3.angle, because Unity said it was Vector3.Angle.
using UnityEngine;
using System.Collections;
public class SwingBlock : $$anonymous$$onoBehaviour {
public float RotateSpeed = 100;
public float angle$$anonymous$$ax = 30.0f;
private Transform SwingRoot;
//private Transform EndRoot;
private Vector3 origAngle;
private float curAngle;
void Start () {
SwingRoot = this.transform.parent.gameObject.transform;
Vector3 origAngle = SwingRoot.position;
}
void Update () {
//Rotation controller
//transform.RotateAround (SwingRoot.position, Vector3.forward, RotateSpeed * Time.deltaTime);
float curAngle = Vector3.Angle(SwingRoot.position, origAngle);
Vector3 rotAngle;
if(curAngle >= angle$$anonymous$$ax){
rotAngle = -1.0f * Vector3.forward;
}
else{
rotAngle = Vector3.forward;
}
transform.RotateAround (SwingRoot.position, rotAngle, RotateSpeed * Time.deltaTime);
}
}