Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 asecretspot · Nov 07, 2014 at 07:31 PM · c#simple

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

}

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

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.

Comment
Add comment · Show 4 · 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 · Nov 07, 2014 at 08:10 PM 0
Share

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.

avatar image asecretspot · Nov 07, 2014 at 08:24 PM 0
Share

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

avatar image Bunny83 · Nov 07, 2014 at 11:32 PM 0
Share

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

avatar image Habitablaba · Nov 08, 2014 at 12:02 AM 0
Share

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

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

4 People are following this question.

avatar image avatar image avatar image avatar image

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

Script doesn't enable even when enabled = true 0 Answers


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