I’m working on a system for my game where the player can attach an object to another where they have to turn a set number of bolts to finish combining the two objects. For example bolting an engine header to the engine block. My issue is I’m not sure where to start with this. So far I just have it to where the player can attach two object together no bolting involved. Can anyone help me figure out how to go about this? I can’t seem to find any tutorials any where.
Sorry, I tried to post this as a comment but it wouldn’t let me. I am trying to do something similar
I have started with this:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
//WIP
public class ToolFastnerJoint : MonoBehaviour
{
// The name of the object that we want to create the configurable joint with (i.e. the nut or bolt)
public string jointObjectName;
// The rotation (in degrees) that the nut or bolt needs to reach before the joint is removed and the event is triggered
public float targetRotation;
public UnityEvent onRotated;
// The configurable joint component that we will create when the collision occurs
private ConfigurableJoint configurableJoint;
// The current rotation of the nut or bolt
private float currentRotation;
// A reference to the nut or bolt object
private GameObject jointObject;
private void OnTriggerEnter(Collider collision)
{
// Check if the colliding object has the name we're looking for
if (collision.gameObject.name.ToLower().IndexOf(jointObjectName.ToLower()) > -1)
{
// Set the joint object to the colliding object
jointObject = collision.gameObject;
// Create a new configurable joint component
configurableJoint = jointObject.AddComponent<ConfigurableJoint>();
// Set the configurable joint's anchor to the center of the nut or bolt
configurableJoint.anchor = jointObject.transform.position;
// Set the configurable joint's connected body to the wrench, and set its anchor to the center of the wrench
configurableJoint.connectedBody = gameObject.GetComponent<Rigidbody>();
configurableJoint.connectedAnchor = gameObject.transform.position;
// Set the configurable joint's rotation axis to the z-axis, and enable rotation around the z-axis
configurableJoint.axis = new Vector3(0, 0, 1);
configurableJoint.angularXMotion = ConfigurableJointMotion.Limited;
configurableJoint.angularYMotion = ConfigurableJointMotion.Free;
configurableJoint.angularZMotion = ConfigurableJointMotion.Free;
}
}
void Update()
{
// Check if we have a reference to a joint object and a configurable joint component
if (jointObject && configurableJoint)
{
// Get the current rotation of the nut or bolt
currentRotation = configurableJoint.connectedBody.transform.localEulerAngles.z;
// Check if the nut or bolt has reached the target rotation
if (currentRotation >= targetRotation)
{
// Remove the configurable joint
Destroy(configurableJoint);
// Trigger the Unity event
onRotated.Invoke();
}
}
}
}
But IU can’t get it to work. Have you made any progress?