- Home /
Simple pendulum swinging script trouble
I found a simple pendulum script here. It works well for what I plan on using it for, except for where the pendulum starts swinging. Its a very basic script and I haven't been able to find out how to change where the pendulum is starting to swing without messing up the timing. The pendulum is doing a 180 degree swing and I want it to start on the side rather than in the middle. Oh and don't mind the bools, the pendulum is activated by another script. If I take off the /2.0f at the end of the Lerp the swinging will start in the right place but then the pendulum's timing will be off and it won't swing smoothly. Any help would be greatly appreciated.
using UnityEngine;
using System.Collections;
public class Pend : MonoBehaviour {
public float angle = 90.0f;
public float speed = 1.5f;
public bool activateR;
public bool activateL;
Quaternion qStart, qEnd;
private float startTime;
void Start () {
qStart = Quaternion.AngleAxis ( angle, Vector3.forward);
qEnd = Quaternion.AngleAxis (-angle, Vector3.forward);
}
void Update (){
if (activateR == true){
startTime += Time.deltaTime;
transform.rotation = Quaternion.Lerp (qStart, qEnd,(Mathf.Sin(startTime * speed) + 1.0f)/ 2.0f);
}
if(activateR == false && activateL == false){
resetTimer ();
}
}
void resetTimer(){
startTime = 0.0f;
}
}
Answer by Kiwasi · Nov 08, 2014 at 12:34 AM
Add the offset to the angle in the sin function. Like so.
transform.rotation = Quaternion.Lerp (qStart, qEnd,(Mathf.Sin(startTime * speed + Mathf.PI/2) + 1.0f)/ 2.0f);
Answer by Bunny83 · Nov 07, 2014 at 07:55 PM
Uhm, how far it is swinging is defined by the angle variable. At the moment you have 90° so it's doing a +90° to -90° swing. If you want say 60° (120° in total) set it to 60°. Since the quaternions are calculated once in start you have to set it in the editor. Setting it at runtime won't change anything.
Just want to add that a real Pendulum doesn't simply follow a sin or cos function. That is a good approximation around 0° (+-30°). Everything above will behave way different. It's best noticable at angles above 90°. The closer you get to 180° the more you will notice it.
So for small angles the above method is a good approximation.
$$anonymous$$aybe I am misunderstanding you but my problem isn't how far it is swinging. Its swinging back and forth to my liking except for how it begins at 0 ins$$anonymous$$d of 90 or -90.
@asecretspot: If you want to have the center of the swinging motion somewhere else the easiest way is to use a child object which is your actual (visual) object. That chils object can be pre-rotated the way you like. As alternative you could add another float variable called "offset" and initialize your quaternions like this:
qStart = Quaternion.AngleAxis (offset +angle, Vector3.forward);
qEnd = Quaternion.AngleAxis (offset -angle, Vector3.forward);
However if you just want to move along the same way but start at a different position, just replace:
startTime = 0.0f;
with
startTime = $$anonymous$$athf.PI/speed;
The way you increase the startTime variable makes it necessary to divide by speed.
If you replace the increment with this:
startTime += Time.deltaTime * speed;
you can remove the " * speed" in line 15. Then you would need
startTime = $$anonymous$$athf.PI;
If that doesn't contain what you want, you need to post an image and describe more clearly what you want.
Yeah, a picture would be great.
The way I read the question, the pendulum starts here:
x
|
|
0
and he wants it to start here:
0----x
Where the 'x' is the same spot in both cases, and the '0' is the weight at the end of the pendulum. Hooray for ascii art.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Multiple Cars not working 1 Answer
WaitForSeconds/Yield problem 1 Answer