- Home /
Attaching 2 objects using joints and raycasting
Hi,
I'd like to attach two rigidbodies together and the connection point must be determined by the player (Mouse click position). How can I do that ?
Here's my current code (It creates a joint if the object is a rigidbody and is being touched by the raycast, that's it)
void Update() {
if(Input.GetMouseButtonDown (1)){
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100)){
Debug.DrawLine(ray.origin, hit.point, Color.cyan);
if(hit.rigidbody != null && !hit.collider.gameObject.GetComponent<ConfigurableJoint>()){
ConfigurableJoint joint = hit.collider.gameObject.AddComponent<ConfigurableJoint>();
joint.xMotion = ConfigurableJointMotion.Locked;
joint.yMotion = ConfigurableJointMotion.Locked;
joint.zMotion = ConfigurableJointMotion.Locked;
joint.enableCollision = true;
}
}
}
}
Create a test scene. Attach two rigidbodies together and fiddle with the values until you're satisfied. Transfer the necessary changes you made into your code here.
I've already done that. But I don't know how I can do it using raycasts.
Your answer
Follow this Question
Related Questions
2.5D Shooting Rigidbodies vs RayCasts 0 Answers
remove forward component from velocity vector 2 Answers
Adding Rigidbody to an Object on Collision by Raycast? 1 Answer
0 Understanding of raycast2D commands 2 Answers
How can I create a joint between objects which only allows for rotation around an axis? 1 Answer