problem with oscillation of pendulum

hi m very new to unity,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”… please help :face_with_spiral_eyes:

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 think you need to use

from.rotation = Quaternion.Euler(70,0,0); 
and
to.rotation = Quaternion.Euler(-70,0,0);

hey m getting errors at run time, unassigned reference exception, but i dont have anything to assign, i just need my pendulum to oscillate 70 degrees right side and 70 degrees left side… please help me

I solved your problem, using 2 other objects. one named To the other named From, which had been rotated on the x axis 70 and -70 degrees.

I’ll place the project as an attachment, you only use From, To and pendulum objects, and the Rotate script (which is C#)
Ignore the other objects/scripts.603974–21458–$pendulum.rar (130 KB)

hey thanks for your reply,but i did’nt understand your script,i dont want any other gameobjects to be created, i have a model “Pendulum” i just want to oscillate it along x axis to 70 degree and -70 degree, is it possible to do like this, please reply m getting mad :frowning:

I don’t quite know how to work with Quaternions, so this is the solution i came up with (which doesn’t use another transform or object)

using UnityEngine;
using System.Collections;

public class Rotate : MonoBehaviour {
	private float rotateSpeed;
	private float rotAngle;
	private int dir;

	


	
	private float ActualRotation(){//calculate the euler angle of the rotation from 0 to 360
		float actualRotation = 0.0f;
		if ((transform.eulerAngles.x <= 90)  (transform.eulerAngles.y == 0))
			actualRotation = transform.eulerAngles.x;
		if ((transform.eulerAngles.x <= 90)  (transform.eulerAngles.y == 180))
			actualRotation = 180 - transform.eulerAngles.x;
		if ((transform.eulerAngles.x >= 270)  (transform.eulerAngles.y == 180))
			actualRotation = 360 - transform.eulerAngles.x + 180;
		if ((transform.eulerAngles.x >= 270)  (transform.eulerAngles.y == 0))
			actualRotation = transform.eulerAngles.x;
		return actualRotation;
	}
	
	// Use this for initialization
	void Start () {
		rotateSpeed = 70;
		rotAngle = 70;
		dir = 1;
	}
	
	
	
	// Update is called once per frame
	void Update () {
		ActualRotation(); //get rotation of the pendulum
		if(ActualRotation() > rotAngle  ActualRotation() < 180)//change rotation dir if it reached rotAngle value (70 deg)
			dir = -1;
		if(ActualRotation() > 180  ActualRotation() < 360 - rotAngle)//change rotation dir if it reached 360 - rotAngle
			dir = 1;
		
		
		
		
		
		
		Debug.Log(ActualRotation());

	
		transform.Rotate(Vector3.right * Time.deltaTime * rotateSpeed * dir);//apply actual rotation
		
		
		
	}
}

I hope it’ll help you.

PS: I am new to Unity

It’s fine to cross-post to Unity Answers, but if you do, please post a link to the question so people aren’t duplicating their work (for instance, there’s a perfectly good answer to this question there).

Oh no we better help you before you get angry, because we wouldn’t like you when you’re angry :stuck_out_tongue: