• 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 navadeep2011 · Mar 13, 2013 at 10:58 AM · motionphysic

Motion script for simple pendulum?

Hi All,

I want make t$$anonymous$$s simple pendulum http://phet.colorado.edu/sims/pendulum-lab/pendulum-lab_en.html please check t$$anonymous$$s. My DragRotate script is working fine.. But i want to give motion depending on that motion drag angle.

By default i will give angle but i want to give some t$$anonymous$$ng like t$$anonymous$$s in the link. my drag rotate code is like t$$anonymous$$s

using UnityEngine; using System.Collections;

public class DragRotate : MonoBehaviour {

 public Transform pivot;
 private float angle;
 private Vector3 v3Pivot;  // Pivot in screen space
 
 void Start () {
    v3Pivot = Camera.main.WorldToScreenPoint (pivot.position);
  }
 
 void OnMouseDown() {
    Vector3 v3T = (Vector3)Input.mousePosition - v3Pivot;
    angle = Mathf.Atan2 (v3T.y, v3T.x) * Mathf.Rad2Deg;
 }
 
 void OnMouseDrag() {
    Vector3 v3T = (Vector3)Input.mousePosition - v3Pivot;
    float angleT  = Mathf.Atan2 (v3T.y, v3T.x)  * Mathf.Rad2Deg;
    float angleDiff = Mathf.DeltaAngle(angle, angleT);
    pivot.Rotate(new Vector3(0.0f, 0.0f, angleDiff));
    angle = angleT;
 }
 

}

and my motion code is like t$$anonymous$$s

using UnityEngine; using System.Collections; public class Pend : MonoBehaviour { private float angle = 0.0f; private float speed = 1.5f;

 Quaternion qStart, qEnd;
 
 void Start () {
    qStart = Quaternion.AngleAxis ( angle, Vector3.forward);
    qEnd   = Quaternion.AngleAxis (-angle, Vector3.forward);
 
 
 }
 
 void Update () 
 {
    angle -= Time.deltaTime;
    if (angle < 0)
      angle = 0;
     qStart = Quaternion.AngleAxis ( angle, Vector3.forward);
     qEnd   = Quaternion.AngleAxis (-angle, Vector3.forward);
     transform.rotation = Quaternion.Lerp (qStart, qEnd, (Mathf.Sin(Time.time * speed) + 1.0f) / 2.0f);
 }

}

but i want to get that angle than due to that i will give motion .. please help me.

Thanks to all.

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
0
Best Answer

Answer by robertbu · Mar 13, 2013 at 10:13 PM

When posting code by others, you need to make reference to the original post or website. Reasons:

  1. When you post code (especially when you say somet$$anonymous$$ng like, "my motion code"), it is implicitly assumed that you both wrote and understand the code you posted. T$$anonymous$$s is not fair to the original authors.

  2. Original posts contain information that often helps inform the solution.

  3. People answering your questions make assumptions about what you know based on the code you post. So the answers are often a mismatch to the level of the programmer when they are asking for help to modify "borrowed" code.

With that said, here is a merge of the two scripts. For it to work, the arm has to be pointed right (angle 0) when there is 0 rotation on the 'Z' axis:

 using UnityEngine;
 using System.Collections;
 
 public class Pendulum : MonoBehaviour {
     
     public Transform pivot;
     public float    speed = 0.5f;
     private Vector3 v3Pivot;  // Pivot in screen space
     private bool    dragging = false;
     private float   startAngle = 0.0f;
     private float   endAngle = -180.0f;
     private float   fTimer = 0.0f;
     private float   angleDiff;
     private Vector3 v3T = Vector3.zero; 
     private float   angle;
 
     void Start () {
         v3Pivot = Camera.main.WorldToScreenPoint (pivot.position);
      }
     
     void Update() {
         if (!dragging) {
             float f = (Mathf.Sin (fTimer * speed - Mathf.PI / 2.0f) + 1.0f) / 2.0f ;
             
             v3T.Set (0.0f, 0.0f, Mathf.Lerp(startAngle, endAngle, f));
             pivot.eulerAngles = v3T;
             fTimer += Time.deltaTime;
         }
     }
     
     void OnMouseDown() {
         dragging = true;
         Vector3 v3T = (Vector3)Input.mousePosition - v3Pivot;
         angle = Mathf.Atan2 (v3T.y, v3T.x) * Mathf.Rad2Deg;
     }
     
     void OnMouseDrag() {
         Vector3 v3T = (Vector3)Input.mousePosition - v3Pivot;
         float angleT  = Mathf.Atan2 (v3T.y, v3T.x)  * Mathf.Rad2Deg;
         angleDiff = Mathf.DeltaAngle(angle, angleT);
         pivot.Rotate(new Vector3(0.0f, 0.0f, angleDiff));
         angle = angleT;
     }
     
     void OnMouseUp() {
         
         Vector3 v3T = Camera.main.WorldToScreenPoint(transform.position); 
         v3T = v3T - v3Pivot;
         float angle = Mathf.Atan2 (v3T.y, v3T.x) * Mathf.Rad2Deg;
         
         Debug.Log (angle);
         
         if (angle < 0.0f) 
             angle += 360.0f;
         
         startAngle = angle;
         
         if (angle <= 90.0f) {
             endAngle = angle - 2.0f * angle - 180.0f;
         }
         else if (angle <= 180.0f) {
             endAngle = angle + 2.0f * (180.0f - angle) + 180.0f;
         }
         else if (angle <= 270.0f) {
             endAngle = angle + 2.0f * (270.0f - angle);
         }
         else {
             endAngle = angle - 2.0f * (angle - 270.0f);
         }
         
         dragging = false;
         fTimer = 0.0f;
     }
 }
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 navadeep2011 · Mar 14, 2013 at 02:17 AM 0
Share

Hi Robertbu.. thanks for helping me. Actually i am in starting position. Today on wards i will follow all the points above you mentioned. once again sorry for my mistake. shankar

avatar image
0

Answer by r0gerg · May 04, 2016 at 06:21 PM

I created a tutorial with the Euler-Cromer method to simulate a simple pendulum. https://rdgamestudio.wordpress.com/2016/05/04/simple-pendulum-in-unity/

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 People are following this question.

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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to import the object from server to unity 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer

Material doesn't have a color property '_Color' 4 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