- Home /
Problem with oscillation
i have a target called "Pendulum Target", i want the pendulum to oscillate 70 degrees to right side and -70 degrees to left side.. i have written the below code,, but m getting errors,, how can i set values to "from" and "To"
var from: Transform ;
var to : Transform ;
var speed = 0.1;
function Update ()
{
from=Quaternion.Euler(70,0, 0);
to=Quaternion.Euler(-70,0, 0);
transform.rotation = Quaternion.Slerp (from.rotation,to.rotation, Time.time * speed);
}
I won't just write it for you, but here's a hint: look at GameObject.transform.rotation and Quaternion.Euler function.
var from: Transform ; var to : Transform ; var speed = 0.1; function Update () { from=Quaternion.Euler(70,0, 0); to=Quaternion.Euler(-70,0, 0); transform.rotation = Quaternion.Slerp (from.rotation,to.rotation, Time.time * speed); } Its giving me error.....
If you'd like to add code to your question, update the original question with the code, so you can use the button with 0's and 1's on it to format the code properly.
No one wants to read unformatted code...
i have a target called "Pendulum Target", i want the pendulum to oscillate 70 degrees to right side and -70 degrees to left side.. i have written the below code,, but m getting errors,, how can i set values to "from" and "To"
var from: Transform ;
var to : Transform ;
var speed = 0.1;
function Update ()
{
from=Quaternion.Euler(70,0, 0);
to=Quaternion.Euler(-70,0, 0);
transform.rotation = Quaternion.Slerp (from.rotation,to.rotation,
Time.time * speed);
}
Ok, good try. You still have a few bugs, but here's a question for you: why do the interpolation from -70 to 70 using Slerp? You are creating the rotations using Euler, why not just interpolate the angle? In other words:
transform.rotation = Quaternion.Euler($$anonymous$$athf.Sin(Time.time) * 70, 0, 0)
Answer by save · Jun 14, 2011 at 03:19 PM
Just a little something based on what hellcats was suggesting (up-vote him instead of this if you find it useful).
var angles : Vector3;
var speed : int = 10;
private var initAngles : Vector3;
function Start () {
initAngles = Vector3(transform.eulerAngles.x, transform.eulerAngles.y, transform.eulerAngles.z);
}
function Update () {
if(angles.x+angles.y+angles.z!=0&&speed>0)
Pendulum(angles.x,angles.y,angles.z);
}
function Pendulum (x,y,z) {
transform.localRotation = Quaternion.Euler(initAngles.x+Mathf.Sin(Time.time*speed)*x, initAngles.y+Mathf.Sin(Time.time*speed)*y, initAngles.z+Mathf.Sin(Time.time*speed)*z);
}
(Updated with initial angles so positioning of objects is easier)
Answer by aldonaletto · Jun 15, 2011 at 08:29 AM
Well, @sanj042, let's try the following:
var ang0:float = 0;
function Update(){
transform.rotation = Quaternion.Euler(Mathf.Sin(Time.time*speed)*70+ang0,0,0);
}
It's basically the same @hellcats and @save suggested, but has the ang0 Inspector var, where you can adjust an initial angle. If your pendulum is oscillating up and down, as you've said, change this to 90 or -90.
In my tests, it oscillated just like an old and good real pendulum, but around it's own center, while in a real pendulum the pivot use to be located at the top. If you experience this problem, do the following:
- Create an empty GameObject and name it "pivot";
- Place the pivot object where you think the pendulum pivot should be;
- Drag your model to the pivot object to make it a child;
- Assign this script to the pivot object (not the pendulum model!)
Good luck!
Answer by Awesome2819 · Mar 19, 2016 at 08:44 AM
public int Omega;
public int Amplitude;
void Update () {
float k = Amplitude * Mathf.Sin(Time.time * Omega);
Quaternion rot = Quaternion.Euler(transform.rotation.eulerAngles.x,ransform.eulerAngles.y,k);
transform.rotation = rot;
}
Omega: Controls speed of oscillation.
Amplitude: Controls how far the pendulum goes to left or right, in this case 70.
Create an empty object, place it on top of the pendulum (i.e where your string starts running from), then make the pendulum child of the empty object you had created and apply this script to the empty object.
This solution has several problems:
Using int variables for Omega and Amplitude doesn't make much sense. Since Omega is basically the speed you can only choose to run at "normal" speed which has the frequency of 0.159 Hz ( which is one period every 6.283 seconds) or a multiple of that frequency. You can't go slower. So use float variables for those settings. For convenience you might want to translate the Omega to a value that represents "periods per second".
Using eulerAngles this way can lead to problems. The eulerangles representation isn't unique. There are always two combinations of angles to get the same rotation. They also suffer from gimbal lock. Unity internally stores the rotation as Quaternion. Whenever you access the eulerangles Unity will convert the Quaternion into eulerangles. This conversion doesn't need to always result in the same eulerangles representation.
So the best approach here would be:
float speed = 1.0f;
float amplitude = 45f;
float offset = 0f;
float phase = 0f;
void Update()
{
float angle = amplitude * $$anonymous$$athf.Sin((Time.time + phase) * $$anonymous$$athf.PI * 2f * speed) + offset;
transform.localRotation = Quaternion.AngleAxis(angle, transform.forward);
}
Where:
speed is in periods per second
amplitude is in degree
offset is also in degree and shifts the 0 point of the rotation
phase is in seconds and is a phase offset which allows you to do a "time-shift" to allow syncing with other Time.time based things.
I also used "localRotation" since for unparented objects it's the same as rotation and slightly faster. However if you parent the pendulum to another object, you can rotate the parent and the pendulum will swing in local space of the parent. So it simplifies the orientation of the pendulum in space.
Your answer
Follow this Question
Related Questions
Z rotation locks to certain value randomly? 0 Answers
puzzle with rotating statues 1 Answer
Turret AI script off rotation 1 Answer