Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sanj042 · Jun 14, 2011 at 05:06 AM · rotationjavascript

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);
 }
Comment
Add comment · Show 16
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image hellcats · Jun 14, 2011 at 05:14 AM 1
Share

I won't just write it for you, but here's a hint: look at GameObject.transform.rotation and Quaternion.Euler function.

avatar image sanj042 · Jun 14, 2011 at 06:09 AM 0
Share

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.....

avatar image CHPedersen · Jun 14, 2011 at 06:27 AM 0
Share

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...

avatar image sanj042 · Jun 14, 2011 at 06:38 AM 0
Share

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);

}

avatar image hellcats · Jun 14, 2011 at 02:04 PM 5
Share

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)

Show more comments

3 Replies

· Add your reply
  • Sort: 
avatar image
2

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)

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image save · Jun 15, 2011 at 09:51 AM 0
Share

Good stuff aldonaletto! :-)

avatar image aldonaletto · Jun 15, 2011 at 01:25 PM 0
Share

Thanks, @save. Please forgive me for having added a new answer to this question (your answer is way more complete!), but @sanj042 seemed so lost and desperate that I felt he would hang himself with all the rope you gave him!

avatar image save · Jun 16, 2011 at 02:30 PM 0
Share

It's all good @aldonaletto! The more the merrier. :-)

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bunny83 · Mar 19, 2016 at 01:01 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Z rotation locks to certain value randomly? 0 Answers

Rotation and collision vectors: How can I make an object turn away from the object it has collided with based on the collision vector? 1 Answer

puzzle with rotating statues 1 Answer

Turret AI script off rotation 1 Answer

rotation LookAt of 2D gameObject in 3D space 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges