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);
}
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!)
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.