- Home /
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.
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.
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.
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613120903im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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