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
2
Question by alanfcf · Oct 29, 2013 at 10:20 PM · jointshingejoint

Hinge Joint via script not working

Hi guys, I have a project (attached, only 120 kb) in which I have a sphere (MenuNode) that creates another five spheres.

I want these five created spheres follow the main sphere (that who creates).

I did that using the Hinge Joint in the editor, but now I want to make it work via script.

Do you see something wrong with this code?

Thats the code:

 using UnityEngine;
 using System.Collections;
 
 public class MenuNodeController : MonoBehaviour {    
     
     //it's all public :)
     public MenuNodeController prefab;
     public bool isSubNode;
     public Vector3 screenPoint;
     public Vector3 offset;
     public Vector3 startPoint;
     public Vector3 endPoint;
     public float startTime;
     public float duration;    
     public static bool touched;    
     
     void Start () {
         
     }
         
     void Update () {
         if (isSubNode) {
             this.transform.position = Vector3.Lerp(startPoint, endPoint, (Time.time - startTime) / duration);
         }
         
         if (Input.GetMouseButtonDown(0)) {
             RaycastHit hit = new RaycastHit();
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             if (Physics.Raycast(ray, out hit)) {    
                 if (!touched) {
                     for (int i = 0; i < 5; i++) {
                         touched = true;
                     
                         MenuNodeController subNode = (MenuNodeController)Instantiate(prefab);
                     
                         subNode.rigidbody.isKinematic = false;
                         subNode.rigidbody.useGravity = false;
                                         
                         subNode.isSubNode = true;
                         subNode.startTime = Time.time;
                         Vector3 endPosition = new Vector3(x(25 * i), y(25 * i), 0);
                         subNode.endPoint = endPosition;
                         subNode.startPoint = this.transform.position;
                         subNode.duration = 2;                        
                         
                         HingeJoint joint = this.gameObject.AddComponent<HingeJoint>();
                         joint.connectedBody = subNode.rigidbody;
                         
                         this.rigidbody.isKinematic = true;
                         this.rigidbody.useGravity = false;                                    
                     }
                 }
             }
         }                        
     }
     
     void OnMouseDown() {
        screenPoint = Camera.main.WorldToScreenPoint(gameObject.transform.position);
        offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z)); 
     }
     
     void OnMouseDrag() {        
         Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
         Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint);
         transform.position = curPosition;
     }
     
     float x(double a) {
         float xa = (float) (Mathf.PI * a/180.0f);
         return 5 * Mathf.Cos(xa);
     }
     
     float y(double a) {
         float ya = (float) (Mathf.PI * a/180.0f);
         return 5 * Mathf.Sin(ya);
     }
 }

The whole project is attached, in this project the hinge joint works when did in the editor.

multiapps.zip (119.4 kB)
Comment
Add comment · Show 2
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 MadMenyo · Mar 13, 2015 at 03:37 PM 0
Share

I have the exact same problem. I have a Pinball flipper attached to a HingeJoint and attached a very basic script. When i manually enter the target position my mesh is moving. When I use the button I hooked up in the script you can see the value change in the inspector but the mesh is not moving.

avatar image stephancom · Apr 06, 2015 at 03:02 AM 0
Share

I seem to be having this same problem, in Unity 5.0

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by kinggryan · May 12, 2015 at 09:48 AM

I've also encountered this and devised a workaround. I'm not sure, but it seems like changing the constraints of a joint do not cause the physics engine to calculate a solution for the scene. By "prodding" the object whose joint's constraints have been changed, you can force a solution. For me, after changing the constraints of a joint, I called AddForce(Vector3.zero) on the object's rigidbody. This caused the new constraints to be enforced.

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 kinggryan · May 12, 2015 at 09:48 AM

I've also encountered this and devised a workaround. I'm not sure, but it seems like changing the constraints of a joint do not cause the physics engine to calculate a solution for the scene. By "prodding" the object whose joint's constraints have been changed, you can force a solution. For me, after changing the constraints of a joint, I called AddForce(Vector3.zero) on the object's rigidbody. This caused the new constraints to be enforced.

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.

Follow this Question

Answers Answers and Comments

18 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Catapult press spacebar to launch / throw 1 Answer

Hinge joint spring variable (c#) 1 Answer

How to add friction to Unity HingeJoint? 1 Answer

How to go about filling the gaps between segmented objects connected with hinge joints? 0 Answers

Hinge Joint with no displacement, strict rotation limits 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