// Add all child indexes to the indexes array recursively private void AddToChildrenRecursive(ConfigurableJoint joint, ref int[] indexes, ref bool[] childFlags) { if (joint == null) { return; } int muscleIndex = GetMuscleIndexLowLevel(joint); if (muscleIndex == -1) { return; } Array.Resize(ref indexes, indexes.Length + 1); indexes[indexes.Length - 1] = muscleIndex; childFlags[muscleIndex] = true; for (int i = 0; i < muscles.Length; i++) { if (i != muscleIndex && muscles[i].joint.connectedBody == joint.GetComponent <Rigidbody>()) { AddToChildrenRecursive(muscles[i].joint, ref indexes, ref childFlags); } } }
void Start() { body = transform.Find("body"); defaultBodyPosY = body.localPosition.y; armR = transform.Find("armR").GetComponent <ConfigurableJoint>(); foreArmR = transform.Find("foreArmR").GetComponent <ConfigurableJoint>(); handR = transform.Find("handR").GetComponent <ConfigurableJoint>(); thighR = transform.Find("thighR").GetComponent <ConfigurableJoint>(); shinR = transform.Find("shinR").GetComponent <ConfigurableJoint>(); footR = transform.Find("footR").GetComponent <ConfigurableJoint>(); armL = transform.Find("armL").GetComponent <ConfigurableJoint>(); foreArmL = transform.Find("foreArmL").GetComponent <ConfigurableJoint>(); handL = transform.Find("handL").GetComponent <ConfigurableJoint>(); thighL = transform.Find("thighL").GetComponent <ConfigurableJoint>(); shinL = transform.Find("shinL").GetComponent <ConfigurableJoint>(); footL = transform.Find("footL").GetComponent <ConfigurableJoint>(); head = transform.Find("head").GetComponent <ConfigurableJoint>(); eyeL = head.transform.Find("eyeL"); eyeR = head.transform.Find("eyeR"); footDisToGround = footL.GetComponent <Collider>().bounds.center.y; bodyHeight = body.GetComponent <Collider>().bounds.size.y; bodyWidth = body.GetComponent <Collider>().bounds.size.z; bpDict = new Dictionary <string, ConfigurableJoint>() { { "armR", armR }, { "foreArmR", foreArmR }, { "handR", handR }, { "thighR", thighR }, { "shinR", shinR }, { "footR", footR }, { "armL", armL }, { "foreArmL", foreArmL }, { "handL", handL }, { "thighL", thighL }, { "shinL", shinL }, { "footL", footL }, { "head", head } }; }
void ShootPole() { if (lastJoint != null) { isActive = false; Destroy(lastJoint); Destroy(GetComponent <ConfigurableJoint>()); lastJoint = null; } Ray ray = new Ray(transform.position, transform.forward); RaycastHit hitinfo; if (Physics.Raycast(ray, out hitinfo, maxHookLength)) { isActive = true; Vector3 vectorChain = hitinfo.point - transform.position; // VectorChain length / chain segment // get hookSegment length from collision box ConfigurableJoint joint = gameObject.AddComponent <ConfigurableJoint>(); joint.xMotion = ConfigurableJointMotion.Locked; joint.yMotion = ConfigurableJointMotion.Locked; joint.zMotion = ConfigurableJointMotion.Locked; joint.anchor = new Vector3(0, 0, 0); // position should be halfway between us and point lastJoint = Instantiate(hookSegment, transform.position + vectorChain * 0.5f, transform.rotation); lastJoint.transform.localScale = new Vector3(lastJoint.transform.localScale.x, lastJoint.transform.localScale.y, vectorChain.magnitude); ConfigurableJoint jointChain = lastJoint.GetComponent <ConfigurableJoint>(); jointChain.anchor = jointChain.anchor; joint.connectedBody = jointChain.GetComponent <Rigidbody>(); } }
public static Vector3 Angles(this ConfigurableJoint joint) { float to180(float v) { if (v > 180) { v = v - 360; } return(v); } Quaternion jointBasis = Quaternion.LookRotation(joint.secondaryAxis, Vector3.Cross(joint.axis, joint.secondaryAxis)); Quaternion jointBasisInverse = Quaternion.Inverse(jointBasis); Vector3 rotation; if (joint.connectedBody != null) { rotation = (jointBasisInverse * Quaternion.Inverse(joint.connectedBody.rotation) * joint.GetComponent <Rigidbody>().transform.rotation *jointBasis).eulerAngles; } else { rotation = (jointBasisInverse * joint.GetComponent <Rigidbody>().transform.rotation *jointBasis).eulerAngles; } return(new Vector3(to180(rotation.x), to180(rotation.z), to180(rotation.y))); }
public static void JointLookAt(ConfigurableJoint joint, Transform pivot, Vector3 destinationWorldPos, Quaternion initialConnectedBodyLocalRotation) { Rigidbody rb = joint.GetComponent <Rigidbody>(); Quaternion desiredWorldRot = Quaternion.LookRotation(destinationWorldPos - pivot.position); Quaternion deltaPivotRot = Quaternion.Inverse(pivot.rotation) * desiredWorldRot; Quaternion rbWorldRot = rb.rotation * deltaPivotRot; if (joint.configuredInWorldSpace) { Quaternion worldToJointSpace = ConfigurableJointExtensions.GetWorldToJointSpace(joint); Quaternion jointSpaceToWorld = Quaternion.Inverse(worldToJointSpace); Quaternion resultRotation = ConfigurableJointExtensions.GetWorldResultRotation(joint, rbWorldRot, initialConnectedBodyLocalRotation, Space.World, jointSpaceToWorld); // Transform back into joint space resultRotation *= worldToJointSpace; // Set target rotation to our newly calculated rotation joint.targetRotation = resultRotation; } else { Quaternion connectedBodyDesiredLocalRot = Quaternion.Inverse(joint.connectedBody.rotation) * rbWorldRot; ConfigurableJointExtensions.SetTargetRotationLocal(joint, connectedBodyDesiredLocalRot, initialConnectedBodyLocalRotation); } }
private void Awake() { _jointBody = _joint.GetComponent <Rigidbody>(); _transform = gameObject.GetComponent <Transform>(); _jointBaseLocalRotation = Quaternion.Inverse(_joint.connectedBody.transform.rotation) * _transform.rotation; _lastLocalRotationJointSpace = Quaternion.Inverse(_jointBaseLocalRotation) * _jointBaseLocalRotation; }
public void SetState(bool On) { ButtonJoint.linearLimit = new SoftJointLimit() { limit = On ? OnLimit : OffLimit }; ButtonOnVisual.SetActive(On); ButtonOffVisual.SetActive(!On); ButtonJoint.GetComponent <Rigidbody>().WakeUp(); }
protected override void ApplyValue(float value) { float num = slideJoint.linearLimit.limit * 2f; float num2 = num * tension / (1f - tension) + num / 2f; slideJoint.targetPosition = new Vector3(Mathf.Lerp(num2, 0f - num2, value), 0f, 0f); JointDrive xDrive = slideJoint.xDrive; xDrive.positionSpring = doorForce / (num2 + num / 2f); xDrive.positionDamper = doorForce / doorSpeed; slideJoint.xDrive = xDrive; slideJoint.GetComponent <Rigidbody>().WakeUp(); }
protected void Awake() { leftDoor = leftJoint.GetComponent <Rigidbody>(); rightDoor = rightJoint.GetComponent <Rigidbody>(); Vector3 position = (leftJoint.transform.position + rightJoint.transform.position) / 2f; ConfigurableJoint configurableJoint = leftJoint; bool autoConfigureConnectedAnchor = false; rightJoint.autoConfigureConnectedAnchor = autoConfigureConnectedAnchor; configurableJoint.autoConfigureConnectedAnchor = autoConfigureConnectedAnchor; leftJoint.anchor = leftJoint.transform.InverseTransformPoint(position); rightJoint.anchor = rightJoint.transform.InverseTransformPoint(position); leftJoint.SetXMotionAnchorsAndLimits(gap / 4f, gap / 2f); rightJoint.SetXMotionAnchorsAndLimits(gap / 4f, gap / 2f); }
// Update is called once per frame void Update() { // simple player movement var h = Input.GetAxis("Horizontal"); var v = Input.GetAxis("Vertical"); transform.Rotate(0f, h * 360f * Time.deltaTime, 0f); cc.Move(transform.forward * v * Time.deltaTime * 10f); // pickup objects if (Input.GetKeyDown(KeyCode.E)) { // TODO: raycast / spherecast, sense which boom we are picking up boomRB = boomHandle.GetComponent <Rigidbody>(); // toggle joint's connected body // we are holding the boom, so DROP IT... if (boomHandle.connectedBody == handsRB) { boomHandle.connectedBody = null; // disconnect joint // snap boom to ground, stabilize boom direction var boom = boomHandle.transform; boom.position = new Vector3(boom.position.x, 0.25f, boom.position.z); boom.eulerAngles = new Vector3(0f, boom.eulerAngles.y, 0f); // turn off physics for the boom boomRB.isKinematic = true; } else { // we are not holding the boom, so PICK IT UP // reset boom orientation to match player boomHandle.transform.position = handsRB.position + transform.TransformDirection(boomOffset); boomHandle.transform.forward = handsRB.transform.forward; // attach boom to player boomHandle.connectedBody = handsRB; boomHandle.connectedAnchor = new Vector3(0f, -0.16f, 0.63f); // as a precaution, zero-out boom physics... but if boom was already kinematic, there was no physics, so this is maybe unnecessary // boomRB.velocity = Vector3.zero; // boomRB.angularVelocity = Vector3.zero; // turn on physics for the boom again boomRB.isKinematic = false; } } }
protected void Awake() { if (slideJoint == null) { slideJoint = GetComponent <ConfigurableJoint>(); } if (slideJoint != null && slideJointRigidBody == null) { slideJointRigidBody = slideJoint.GetComponent <Rigidbody>(); } buttonTransform = slideJoint.transform; slideJoint.SetXMotionAnchorsAndLimits(motionRange / 2f, motionRange); SetForce(buttonForce); inputState = (Mathf.Abs(input.value) >= 0.5f); Snap(inputState); }
// Regather (compound) colliders associated with this muscle. public void UpdateColliders() { var collider = joint.GetComponent <Collider>(); _colliders = new Collider[collider != null? 1: 0]; if (_colliders.Length == 1) { _colliders[0] = collider; } int childCount = joint.transform.childCount; for (int i = 0; i < childCount; i++) { AddCompoundColliders(joint.transform.GetChild(i), ref _colliders); } }
void CheckIfBreak() { if (lastJoint == null) { return; } Ray ray = new Ray(transform.position, (hitPos - transform.position).normalized); RaycastHit hitinfo; if (Physics.Raycast(ray, out hitinfo, maxHookLength)) { Debug.Log("New ray!"); Vector3 vectorChain = hitinfo.point - transform.position; if (vectorChain.magnitude < chainLength + epsilon) { //Kolla Velocityn ta längden på den och sätt den i rätt riktning så att velocityn preservas när vi byter hinge.(JOINT) // New midpos lastJoint.GetComponent <ConfigurableJoint>().xMotion = ConfigurableJointMotion.Free; lastJoint.GetComponent <ConfigurableJoint>().yMotion = ConfigurableJointMotion.Free; lastJoint.GetComponent <ConfigurableJoint>().zMotion = ConfigurableJointMotion.Free; lastJoint.transform.position = hitinfo.point + (hitPos - hitinfo.point) * 0.5f; lastJoint.GetComponent <Rigidbody>().isKinematic = true; lastJoint.transform.localScale = new Vector3(lastJoint.transform.localScale.x, lastJoint.transform.localScale.y, (hitPos - hitinfo.point).magnitude); lastChain.Add(lastJoint); hitPos = hitinfo.point; chainLength = vectorChain.magnitude; ConfigurableJoint joint = gameObject.GetComponent <ConfigurableJoint>(); // position should be halfway between us and point //Transform forVecToQauternion = new Transform();//= ray.direction; //forVecToQauternion.position = ray.direction; lastJoint = Instantiate(hookSegment, transform.position + vectorChain * 0.5f, Quaternion.LookRotation(vectorChain.normalized, Vector3.up)); lastJoint.transform.localScale = new Vector3(lastJoint.transform.localScale.x, lastJoint.transform.localScale.y, vectorChain.magnitude); ConfigurableJoint jointChain = lastJoint.GetComponent <ConfigurableJoint>(); jointChain.anchor = jointChain.anchor; joint.connectedBody = jointChain.GetComponent <Rigidbody>(); } } }
public static void ApplyXMotionTarget(this ConfigurableJoint joint) { Vector3 a = (!(joint.connectedBody != null)) ? joint.connectedAnchor : joint.connectedBody.transform.TransformPoint(joint.connectedAnchor); float limit = joint.linearLimit.limit; Vector3 targetPosition = joint.targetPosition; float num = targetPosition.x; if (limit > 0f) { num = Mathf.Clamp(num, 0f - limit, limit); } Vector3 position = joint.anchor + joint.axis * num; Vector3 b = joint.transform.TransformPoint(position); joint.transform.position += a - b; Rigidbody component = joint.GetComponent <Rigidbody>(); Vector3 vector2 = component.angularVelocity = (component.velocity = Vector3.zero); }
void ShootChain() { foreach (var item in lastChain) { Destroy(item); Destroy(GetComponent <ConfigurableJoint>()); } lastChain.Clear(); isActive = false; Ray ray = new Ray(transform.position, transform.forward); RaycastHit hitinfo; if (Physics.Raycast(ray, out hitinfo, maxHookLength)) { isActive = true; hitPos = hitinfo.point; Vector3 vectorChain = hitinfo.point - transform.position; int numSegments = (int)Mathf.Ceil(vectorChain.magnitude / segmentLength); chainLength = vectorChain.magnitude; ConfigurableJoint prevJoint = gameObject.AddComponent <ConfigurableJoint>(); prevJoint.xMotion = ConfigurableJointMotion.Locked; prevJoint.yMotion = ConfigurableJointMotion.Locked; prevJoint.zMotion = ConfigurableJointMotion.Locked; prevJoint.anchor = new Vector3(0, 0, 0); for (int i = 0; i < numSegments; i++) { // position should be halfway between us and point lastJoint = Instantiate(hookSegment, transform.position + i * vectorChain.normalized * segmentLength + vectorChain.normalized * segmentLength * 0.5f, transform.rotation); lastJoint.transform.localScale = new Vector3(lastJoint.transform.localScale.x, lastJoint.transform.localScale.y, segmentLength); ConfigurableJoint jointChain = lastJoint.GetComponent <ConfigurableJoint>(); jointChain.anchor = jointChain.anchor; prevJoint.connectedBody = jointChain.GetComponent <Rigidbody>(); prevJoint = jointChain; lastChain.Add(jointChain.gameObject); } lastChain[lastChain.Count - 1].GetComponent <BoxCollider>().enabled = false; } }
// ########################### public void InitStage2() { // Connect the joint to a parent rigidbody Transform tran = MyTransform.parent; Rigidbody rb = tran.GetComponent <Rigidbody>(); while ((rb == null) && (tran != null)) { tran = tran.parent; rb = tran.GetComponent <Rigidbody>(); } if (rb == null) { Debug.LogError("Unable to connect fixed joint"); } m_JointWaitingForConnect.connectedBody = rb; // Set the joint control target m_JointWaitingForConnect.GetComponent <JointControl>().SerializeTarget(Target); }
public static void SetSlaveBoneConfiguration(ConfigurableJoint joint, SlaveBoneConfiguration conf) { Rigidbody rb = joint.GetComponent <Rigidbody>(); rb.mass = conf.rigidbodyMass; rb.drag = conf.rigidbodyDrag; rb.angularDrag = conf.rigidbodyAngularDrag; rb.useGravity = conf.useGravity; if (conf.followsRotation) { joint.angularXDrive = conf.rotationDrive.toJointDrive(); joint.angularYZDrive = conf.rotationDrive.toJointDrive(); joint.slerpDrive = conf.rotationDrive.toJointDrive(); } else { joint.angularXDrive = CustomJointDrive.zero.toJointDrive(); joint.angularYZDrive = CustomJointDrive.zero.toJointDrive(); joint.slerpDrive = CustomJointDrive.zero.toJointDrive(); } if (conf.useTargetPos) { joint.xDrive = conf.positionDrive.toJointDrive(); joint.yDrive = conf.positionDrive.toJointDrive(); joint.zDrive = conf.positionDrive.toJointDrive(); } else { joint.xDrive = CustomJointDrive.zero.toJointDrive(); joint.yDrive = CustomJointDrive.zero.toJointDrive(); joint.zDrive = CustomJointDrive.zero.toJointDrive(); } joint.massScale = conf.jointMassScale; joint.connectedMassScale = conf.jointConnectedMassScale; }
/// <summary> /// Removes a previously created attachment joint. /// </summary> public virtual void RemoveAttachmentJoint() { if (attachmentJoint != null) { UnityEngine.Object.Destroy(attachmentJoint.GetComponent <ConfigurableJoint>()); DebugLog("Removed attachment joint"); } if (magnetRigidBody != null) { UnityEngine.Object.Destroy(magnetRigidBody.GetComponent <Rigidbody>()); DebugLog("Removed magnet rigid body"); } //Play the detach effect if (!string.IsNullOrEmpty(detachEffectName)) { this.part.Effect(detachEffectName, 1.0f); } if (!string.IsNullOrEmpty(runningEffectName)) { this.part.Effect(runningEffectName, -1.0f); } }
bool TryCreateRagdollBones() { if (!BindingsAreValid) { if (Application.isPlaying) { UnityEngine.Debug.LogError("Ragdoll Definition Bindings aren't correctly set up.", this); } return(false); } bones = new Dictionary <BoneName, RagdollBone>(); //Create the bones foreach (BoneName boneName in bindings.Keys) { ConfigurableJoint joint = bindings[boneName]; RagdollBone bone = new RagdollBone(boneName, joint.transform, joint.GetComponent <Rigidbody>(), joint, _definition.IsRoot(boneName)); bones.Add(boneName, bone); } return(true); }
/// <summary> /// NB! Make sure to call this from FixedUpdate! /// Creates a new muscle for the specified "joint" and targets it to the "target". The joint will be connected to the specified "connectTo" Muscle. /// Note that the joint will be binded to it's current position and rotation relative to the "connectTo", so make sure the added object is positioned correctly when calling this. /// This method allocates memory, avoid using it each frame. /// </summary> public void AddMuscle(ConfigurableJoint joint, Transform target, Rigidbody connectTo, Transform targetParent, Muscle.Props muscleProps = null, bool forceTreeHierarchy = false, bool forceLayers = true) { if (!CheckIfInitiated()) { return; } if (!initiated) { Debug.LogWarning("PuppetMaster has not been initiated.", transform); return; } if (ContainsJoint(joint)) { Debug.LogWarning("Joint " + joint.name + " is already used by a Muscle", transform); return; } if (target == null) { Debug.LogWarning("AddMuscle was called with a null 'target' reference.", transform); return; } if (connectTo == joint.GetComponent <Rigidbody>()) { Debug.LogWarning("ConnectTo is the joint's own Rigidbody, can not add muscle.", transform); return; } if (!isActive) { Debug.LogWarning("Adding muscles to inactive PuppetMasters is not currently supported.", transform); return; } if (muscleProps == null) { muscleProps = new Muscle.Props(); } Muscle muscle = new Muscle(); muscle.props = muscleProps; muscle.joint = joint; muscle.target = target; muscle.joint.transform.parent = (hierarchyIsFlat || connectTo == null) && !forceTreeHierarchy? transform: connectTo.transform; if (forceLayers) { joint.gameObject.layer = gameObject.layer; //@todo what if collider is on a child gameobject? target.gameObject.layer = targetRoot.gameObject.layer; } if (connectTo != null) { muscle.target.parent = targetParent; Vector3 relativePosition = GetMuscle(connectTo).transform.InverseTransformPoint(muscle.target.position); Quaternion relativeRotation = Quaternion.Inverse(GetMuscle(connectTo).transform.rotation) * muscle.target.rotation; joint.transform.position = connectTo.transform.TransformPoint(relativePosition); joint.transform.rotation = connectTo.transform.rotation * relativeRotation; joint.connectedBody = connectTo; } muscle.Initiate(muscles); if (connectTo != null) { muscle.rigidbody.velocity = connectTo.velocity; muscle.rigidbody.angularVelocity = connectTo.angularVelocity; } // Ignore internal collisions if (!internalCollisions) { for (int i = 0; i < muscles.Length; i++) { muscle.IgnoreCollisions(muscles[i], true); } } Array.Resize(ref muscles, muscles.Length + 1); muscles[muscles.Length - 1] = muscle; // Update angular limit ignoring muscle.IgnoreAngularLimits(!angularLimits); if (behaviours.Length > 0) { muscle.broadcaster = muscle.joint.gameObject.AddComponent <MuscleCollisionBroadcaster>(); muscle.broadcaster.puppetMaster = this; muscle.broadcaster.muscleIndex = muscles.Length - 1; } muscle.jointBreakBroadcaster = muscle.joint.gameObject.AddComponent <JointBreakBroadcaster>(); muscle.jointBreakBroadcaster.puppetMaster = this; muscle.jointBreakBroadcaster.muscleIndex = muscles.Length - 1; UpdateHierarchies(); CheckMassVariation(100f, true); foreach (BehaviourBase b in behaviours) { b.OnMuscleAdded(muscle); } }
public void DropNotLight() { // toggle joint's connected body //we are holding the boom, so DROP IT... if (oscar.connectedBody == handsRB) { oscar.connectedBody = null; // disconnect joint // snap boom to ground, stabilize boom direction Rigidbody rb = oscar.GetComponent <Rigidbody>(); Destroy(oscar); StartCoroutine("CreateJointOscar"); //foreach(System.Reflection.FieldInfo field in boomHandle1.GetType().GetFields()) //{ // field.SetValue(oscar, field.GetValue(boomHandle1.GetType())); //} //rb.AddForce(breakForce, breakForce, breakForce); } if (table.connectedBody == handsRB) { table.connectedBody = null; // disconnect joint // snap boom to ground, stabilize boom direction Rigidbody rb = table.GetComponent <Rigidbody>(); Destroy(table); StartCoroutine("CreateJointTable"); //foreach(System.Reflection.FieldInfo field in boomHandle1.GetType().GetFields()) //{ // field.SetValue(oscar, field.GetValue(boomHandle1.GetType())); //} //rb.AddForce(breakForce, breakForce, breakForce); } if (chair.connectedBody == handsRB) { chair.connectedBody = null; // disconnect joint // snap boom to ground, stabilize boom direction Rigidbody rb = chair.GetComponent <Rigidbody>(); Destroy(chair); StartCoroutine("CreateJointChair"); //foreach(System.Reflection.FieldInfo field in boomHandle1.GetType().GetFields()) //{ // field.SetValue(oscar, field.GetValue(boomHandle1.GetType())); //} //rb.AddForce(breakForce, breakForce, breakForce); } if (directorChair.connectedBody == handsRB) { directorChair.connectedBody = null; // disconnect joint // snap boom to ground, stabilize boom direction Rigidbody rb = directorChair.GetComponent <Rigidbody>(); Destroy(directorChair); StartCoroutine("CreateJointDirector"); //foreach(System.Reflection.FieldInfo field in boomHandle1.GetType().GetFields()) //{ // field.SetValue(oscar, field.GetValue(boomHandle1.GetType())); //} //rb.AddForce(breakForce, breakForce, breakForce); } if (panda.connectedBody == handsRB) { panda.connectedBody = null; // disconnect joint // snap boom to ground, stabilize boom direction Rigidbody rb = panda.GetComponent <Rigidbody>(); Destroy(panda); StartCoroutine("CreateJointPanda"); //foreach(System.Reflection.FieldInfo field in boomHandle1.GetType().GetFields()) //{ // field.SetValue(oscar, field.GetValue(boomHandle1.GetType())); //} //rb.AddForce(breakForce, breakForce, breakForce); } if (panda1.connectedBody == handsRB) { panda1.connectedBody = null; // disconnect joint // snap boom to ground, stabilize boom direction Rigidbody rb = panda1.GetComponent <Rigidbody>(); Destroy(panda1); StartCoroutine("CreateJointPanda1"); //foreach(System.Reflection.FieldInfo field in boomHandle1.GetType().GetFields()) //{ // field.SetValue(oscar, field.GetValue(boomHandle1.GetType())); //} //rb.AddForce(breakForce, breakForce, breakForce); } if (chair1.connectedBody == handsRB) { chair1.connectedBody = null; // disconnect joint // snap boom to ground, stabilize boom direction Rigidbody rb = chair1.GetComponent <Rigidbody>(); Destroy(chair1); StartCoroutine("CreateJointChair1"); //foreach(System.Reflection.FieldInfo field in boomHandle1.GetType().GetFields()) //{ // field.SetValue(oscar, field.GetValue(boomHandle1.GetType())); //} //rb.AddForce(breakForce, breakForce, breakForce); } if (table1.connectedBody == handsRB) { table1.connectedBody = null; // disconnect joint // snap boom to ground, stabilize boom direction Rigidbody rb = table1.GetComponent <Rigidbody>(); Destroy(table1); StartCoroutine("CreateJointTable1"); //foreach(System.Reflection.FieldInfo field in boomHandle1.GetType().GetFields()) //{ // field.SetValue(oscar, field.GetValue(boomHandle1.GetType())); //} //rb.AddForce(breakForce, breakForce, breakForce); } // turn off physics for the booms //boomRB.isKinematic = true; rbody = false; isHeld = false; itemGrabbed = null; }
/// <summary> /// Initialize the joint /// </summary> /// <param name="rotate">When set to true, the joint is rotated, else false</param> private void InitJoint() { if ((part.attachJoint == null) || (activeReference == -1)) { return; } //Destroy the joint if it still exists if (joint != null) { //Debug.Log("[LYNX] InitJoint: Destroying"); DestroyImmediate(joint); } //Save the original joint for later if (part.attachJoint != null) { originalJoint = part.attachJoint.Joint; } //Catch reversed joint (TODO voodoo?) if (transform.position != part.attachJoint.Joint.connectedBody.transform.position) { joint = part.attachJoint.Joint.connectedBody.gameObject.AddComponent <ConfigurableJoint>(); joint.connectedBody = part.attachJoint.Joint.GetComponent <Rigidbody>(); } else { joint = part.attachJoint.Joint.GetComponent <Rigidbody>().gameObject.AddComponent <ConfigurableJoint>(); joint.connectedBody = part.attachJoint.Joint.connectedBody; } if (joint == null) { Debug.Log("[LYNX] ERR InitJoint: Cannot create joint!"); } //Make the new joint unbreakable joint.breakForce = float.PositiveInfinity; joint.breakTorque = float.PositiveInfinity; //Make the original joint unbreakable part.attachJoint.Joint.breakForce = float.PositiveInfinity; part.attachJoint.Joint.breakTorque = float.PositiveInfinity; part.attachJoint.SetBreakingForces(float.PositiveInfinity, float.PositiveInfinity); //Lock the movement of the joint joint.xMotion = ConfigurableJointMotion.Locked; joint.yMotion = ConfigurableJointMotion.Locked; joint.zMotion = ConfigurableJointMotion.Locked; //Set projection distance and angle joint.projectionDistance = 0f; joint.projectionAngle = 0f; joint.projectionMode = JointProjectionMode.PositionAndRotation; //Copy drives joint.linearLimit = part.attachJoint.Joint.linearLimit; joint.angularXDrive = part.attachJoint.Joint.angularXDrive; joint.angularYZDrive = part.attachJoint.Joint.angularYZDrive; joint.xDrive = part.attachJoint.Joint.xDrive; joint.yDrive = part.attachJoint.Joint.yDrive; joint.zDrive = part.attachJoint.Joint.zDrive; //Get the rigid body of the joint Rigidbody jointRigidBody = joint.GetComponent <Rigidbody>(); //Set anchor position joint.anchor = jointRigidBody.transform.InverseTransformPoint(joint.connectedBody.transform.position); joint.connectedAnchor = Vector3.zero; //Set the right rotation of the joint (we have to invert x and y...hell knows why) if (activeReference != -1) { joint.transform.rotation = (joint.transform.rotation * Quaternion.Euler(rotationDeltas[activeReference].eulerAngles.x, rotationDeltas[activeReference].eulerAngles.y, rotationDeltas[activeReference].eulerAngles.z)); } //Set correct axis joint.axis = jointRigidBody.transform.InverseTransformDirection(joint.connectedBody.transform.right); //x axis joint.secondaryAxis = jointRigidBody.transform.InverseTransformDirection(joint.connectedBody.transform.up); //y axis //Disable the collision joint.enableCollision = false; //Set the drive mode joint.rotationDriveMode = RotationDriveMode.XYAndZ; }
public void Pickup() { RaycastHit hit; Ray ray = new Ray(camera.transform.position, camera.transform.forward); Debug.DrawRay(ray.origin, ray.direction * 1000, Color.red); print("pickup called"); if (Physics.Raycast(ray, out hit, rayDistance)) { if (hit.collider != null) { var localPoint = hit.textureCoord; Ray ray2 = rcamera.ScreenPointToRay(new Vector2(localPoint.x * rcamera.pixelWidth, localPoint.y * rcamera.pixelHeight)); RaycastHit rhit; if (Physics.Raycast(ray, out rhit, rayDistance)) { print(hit.collider.name); if (rhit.collider.tag == "light1") { rbody = true; isHeld = true; Debug.Log("light1"); itemGrabbed = rhit.collider.gameObject; boomRB = boomHandle1.GetComponent <Rigidbody>(); itemGrabbed = null; // we are not holding the boom, so PICK IT UP // reset boom orientation to match player boomHandle1.transform.position = handsRB.position + transform.TransformDirection(boomOffset); boomHandle1.transform.forward = handsRB.transform.forward; // attach boom to player boomHandle1.connectedBody = handsRB; boomHandle1.connectedAnchor = new Vector3(0f, -0.16f, 0.63f); // as a precaution, zero-out boom physics... but if boom was already kinematic, there was no physics, so this is maybe unnecessary // boomRB.velocity = Vector3.zero; // boomRB.angularVelocity = Vector3.zero; // turn on physics for the boom again boomRB.isKinematic = false; light = true; AudioSource audioPick = GetComponent <AudioSource>(); audioPick.clip = pickupSound; audioPick.Play(); } if (rhit.collider.tag == "light2") { rbody = true; isHeld = true; Debug.Log("light2"); itemGrabbed = rhit.collider.gameObject; boomRB = boomHandle2.GetComponent <Rigidbody>(); itemGrabbed = null; // we are not holding the boom, so PICK IT UP // reset boom orientation to match player boomHandle2.transform.position = handsRB.position + transform.TransformDirection(boomOffset); boomHandle2.transform.forward = handsRB.transform.forward; // attach boom to player boomHandle2.connectedBody = handsRB; boomHandle2.connectedAnchor = new Vector3(0f, -0.16f, 0.63f); // boomRB.angularVelocity = Vector3.zero; // turn on physics for the boom again boomRB.isKinematic = false; light = true; AudioSource audioPick = GetComponent <AudioSource>(); audioPick.clip = pickupSound; audioPick.Play(); } if (rhit.collider.tag == "light3") { rbody = true; isHeld = true; Debug.Log("light3"); itemGrabbed = rhit.collider.gameObject; boomRB = boomHandle3.GetComponent <Rigidbody>(); itemGrabbed = null; // we are not holding the boom, so PICK IT UP // reset boom orientation to match player boomHandle3.transform.position = handsRB.position + transform.TransformDirection(boomOffset); boomHandle3.transform.forward = handsRB.transform.forward; // attach boom to player boomHandle3.connectedBody = handsRB; boomHandle3.connectedAnchor = new Vector3(0f, -0.16f, 0.63f); // as a precaution, zero-out boom physics... but if boom was already kinematic, there was no physics, so this is maybe unnecessary // boomRB.velocity = Vector3.zero; // boomRB.angularVelocity = Vector3.zero; // turn on physics for the boom again boomRB.isKinematic = false; light = true; AudioSource audioPick = GetComponent <AudioSource>(); audioPick.clip = pickupSound; audioPick.Play(); } if (rhit.collider.tag == "oscar") { rbody = true; isHeld = true; Debug.Log("oscar"); itemGrabbed = rhit.collider.gameObject; boomRB = oscar.GetComponent <Rigidbody>(); itemGrabbed = null; // we are not holding the boom, so PICK IT UP // reset boom orientation to match player oscar.transform.position = handsRB.position + transform.TransformDirection(boomOffset); oscar.transform.forward = handsRB.transform.forward; // attach boom to player oscar.connectedBody = handsRB; oscar.connectedAnchor = new Vector3(0f, -0.16f, 0.63f); // boomRB.angularVelocity = Vector3.zero; // turn on physics for the boom again boomRB.isKinematic = false; AudioSource audioPick = GetComponent <AudioSource>(); audioPick.clip = pickupSound; audioPick.Play(); } if (rhit.collider.tag == "table") { rbody = true; isHeld = true; Debug.Log("table"); itemGrabbed = rhit.collider.gameObject; boomRB = table.GetComponent <Rigidbody>(); itemGrabbed = null; // we are not holding the boom, so PICK IT UP // reset boom orientation to match player table.transform.position = handsRB.position + transform.TransformDirection(boomOffset); table.transform.forward = handsRB.transform.forward; // attach boom to player table.connectedBody = handsRB; table.connectedAnchor = new Vector3(0f, -0.16f, 0.63f); // boomRB.angularVelocity = Vector3.zero; // turn on physics for the boom again boomRB.isKinematic = false; AudioSource audioPick = GetComponent <AudioSource>(); audioPick.clip = pickupSound; audioPick.Play(); } if (rhit.collider.tag == "chair") { rbody = true; isHeld = true; Debug.Log("chair"); itemGrabbed = rhit.collider.gameObject; boomRB = chair.GetComponent <Rigidbody>(); itemGrabbed = null; // we are not holding the boom, so PICK IT UP // reset boom orientation to match player chair.transform.position = handsRB.position + transform.TransformDirection(boomOffset); chair.transform.forward = handsRB.transform.forward; // attach boom to player chair.connectedBody = handsRB; chair.connectedAnchor = new Vector3(0f, -0.16f, 0.63f); // boomRB.angularVelocity = Vector3.zero; // turn on physics for the boom again boomRB.isKinematic = false; AudioSource audioPick = GetComponent <AudioSource>(); audioPick.clip = pickupSound; audioPick.Play(); } if (rhit.collider.tag == "director chair") { rbody = true; isHeld = true; Debug.Log("chair"); itemGrabbed = rhit.collider.gameObject; boomRB = directorChair.GetComponent <Rigidbody>(); itemGrabbed = null; // we are not holding the boom, so PICK IT UP // reset boom orientation to match player directorChair.transform.position = handsRB.position + transform.TransformDirection(boomOffset); directorChair.transform.forward = handsRB.transform.forward; // attach boom to player directorChair.connectedBody = handsRB; directorChair.connectedAnchor = new Vector3(0f, -0.16f, 0.63f); // boomRB.angularVelocity = Vector3.zero; // turn on physics for the boom again boomRB.isKinematic = false; AudioSource audioPick = GetComponent <AudioSource>(); audioPick.clip = pickupSound; audioPick.Play(); } if (rhit.collider.tag == "panda") { rbody = true; isHeld = true; Debug.Log("panda"); itemGrabbed = rhit.collider.gameObject; boomRB = panda.GetComponent <Rigidbody>(); itemGrabbed = null; // we are not holding the boom, so PICK IT UP // reset boom orientation to match player panda.transform.position = handsRB.position + transform.TransformDirection(boomOffset); panda.transform.forward = handsRB.transform.forward; // attach boom to player panda.connectedBody = handsRB; panda.connectedAnchor = new Vector3(0f, -0.16f, 0.63f); // boomRB.angularVelocity = Vector3.zero; // turn on physics for the boom again boomRB.isKinematic = false; AudioSource audioPick = GetComponent <AudioSource>(); audioPick.clip = pickupSound; audioPick.Play(); } if (rhit.collider.tag == "panda1") { rbody = true; isHeld = true; Debug.Log("panda1"); itemGrabbed = rhit.collider.gameObject; boomRB = panda1.GetComponent <Rigidbody>(); itemGrabbed = null; // we are not holding the boom, so PICK IT UP // reset boom orientation to match player panda1.transform.position = handsRB.position + transform.TransformDirection(boomOffset); panda1.transform.forward = handsRB.transform.forward; // attach boom to player panda1.connectedBody = handsRB; panda1.connectedAnchor = new Vector3(0f, -0.16f, 0.63f); // boomRB.angularVelocity = Vector3.zero; // turn on physics for the boom again boomRB.isKinematic = false; AudioSource audioPick = GetComponent <AudioSource>(); audioPick.clip = pickupSound; audioPick.Play(); } if (rhit.collider.tag == "chair1") { rbody = true; isHeld = true; Debug.Log("chair1"); itemGrabbed = rhit.collider.gameObject; boomRB = chair1.GetComponent <Rigidbody>(); itemGrabbed = null; // we are not holding the boom, so PICK IT UP // reset boom orientation to match player chair1.transform.position = handsRB.position + transform.TransformDirection(boomOffset); chair1.transform.forward = handsRB.transform.forward; // attach boom to player chair1.connectedBody = handsRB; chair1.connectedAnchor = new Vector3(0f, -0.16f, 0.63f); // boomRB.angularVelocity = Vector3.zero; // turn on physics for the boom again boomRB.isKinematic = false; AudioSource audioPick = GetComponent <AudioSource>(); audioPick.clip = pickupSound; audioPick.Play(); } if (rhit.collider.tag == "table1") { rbody = true; isHeld = true; Debug.Log("table1"); itemGrabbed = rhit.collider.gameObject; boomRB = table1.GetComponent <Rigidbody>(); itemGrabbed = null; // we are not holding the boom, so PICK IT UP // reset boom orientation to match player table1.transform.position = handsRB.position + transform.TransformDirection(boomOffset); table1.transform.forward = handsRB.transform.forward; // attach boom to player table1.connectedBody = handsRB; table1.connectedAnchor = new Vector3(0f, -0.16f, 0.63f); // boomRB.angularVelocity = Vector3.zero; // turn on physics for the boom again boomRB.isKinematic = false; AudioSource audioPick = GetComponent <AudioSource>(); audioPick.clip = pickupSound; audioPick.Play(); } } } } }
/// <summary> /// Adds a PropMuscle to the puppet at runtime. If Vector3.zero passed for additionalPinOffset, additional pin will not be added. /// </summary> public bool AddPropMuscle(ConfigurableJoint addPropMuscleTo, Vector3 position, Quaternion rotation, Vector3 additionalPinOffset, Transform targetParent = null, PuppetMasterProp initiateWithProp = null) { if (!initiated) { Debug.LogError("Can not add Prop Muscle to PuppetMaster that has not been initiated! Please use Start() instead of Awake() or PuppetMaster.OnPostInitiate delegate to call AddPropMuscle.", transform); return(false); } if (addPropMuscleTo != null) { bool isFlat = HierarchyIsFlat(); var addToMuscle = GetMuscle(addPropMuscleTo); if (addToMuscle != null) { GameObject go = new GameObject("Prop Muscle " + addPropMuscleTo.name); go.layer = addPropMuscleTo.gameObject.layer; go.transform.parent = isFlat ? transform : addPropMuscleTo.transform; go.transform.position = position; go.transform.rotation = rotation; go.AddComponent <Rigidbody>(); GameObject target = new GameObject("Prop Muscle Target " + addPropMuscleTo.name); target.gameObject.layer = addToMuscle.target.gameObject.layer; target.transform.parent = targetParent != null? targetParent: addToMuscle.target; target.transform.position = go.transform.position; target.transform.rotation = go.transform.rotation; ConfigurableJoint joint = go.AddComponent <ConfigurableJoint>(); joint.xMotion = ConfigurableJointMotion.Locked; joint.yMotion = ConfigurableJointMotion.Locked; joint.zMotion = ConfigurableJointMotion.Locked; joint.angularXMotion = ConfigurableJointMotion.Locked; joint.angularYMotion = ConfigurableJointMotion.Locked; joint.angularZMotion = ConfigurableJointMotion.Locked; Muscle.Props props = new Muscle.Props(); props.group = Muscle.Group.Prop; AddMuscle(joint, target.transform, addPropMuscleTo.GetComponent <Rigidbody>(), targetParent != null ? targetParent : addToMuscle.target, props, false, true); muscles[muscles.Length - 1].isPropMuscle = true; var propMuscle = go.AddComponent <PropMuscle>(); propMuscle.puppetMaster = this; propMuscle.additionalPinOffset = additionalPinOffset; propMuscle.currentProp = initiateWithProp; if (additionalPinOffset != Vector3.zero) { propMuscle.AddAdditionalPin(); } Array.Resize(ref propMuscles, propMuscles.Length + 1); propMuscles[propMuscles.Length - 1] = propMuscle; propMuscle.OnInitiate(); return(true); } else { Debug.LogError("Can't add Prop Muscle to a ConfigurableJoint that is not in the list of PuppetMaster.muscles.", transform); return(false); } } else { Debug.LogError("Please assign the ConfigurableJoint of the muscle you wish to add the Prop Muscle to.", transform); return(false); } }
public void SetupJoint() { savedJoint = part.attachJoint.Joint; // Catch reversed joint // Maybe there is a best way to do it? if (transform.position != part.attachJoint.Joint.connectedBody.transform.position) { joint = part.attachJoint.Joint.connectedBody.gameObject.AddComponent <ConfigurableJoint>(); joint.connectedBody = part.attachJoint.Joint.GetComponent <Rigidbody>(); } else { joint = part.attachJoint.Joint.GetComponent <Rigidbody>().gameObject.AddComponent <ConfigurableJoint>(); joint.connectedBody = part.attachJoint.Joint.connectedBody; } joint.breakForce = 1e15f; joint.breakTorque = 1e15f; // And to default joint part.attachJoint.Joint.breakForce = 1e15f; part.attachJoint.Joint.breakTorque = 1e15f; part.attachJoint.SetBreakingForces(1e15f, 1e15f); joint.xMotion = ConfigurableJointMotion.Free; joint.yMotion = ConfigurableJointMotion.Free; joint.zMotion = ConfigurableJointMotion.Free; joint.angularXMotion = ConfigurableJointMotion.Free; joint.angularYMotion = ConfigurableJointMotion.Free; joint.angularZMotion = ConfigurableJointMotion.Free; joint.projectionDistance = 0f; joint.projectionAngle = 0f; joint.projectionMode = JointProjectionMode.PositionAndRotation; // Copy drives joint.linearLimit = part.attachJoint.Joint.linearLimit; joint.lowAngularXLimit = part.attachJoint.Joint.lowAngularXLimit; joint.highAngularXLimit = part.attachJoint.Joint.highAngularXLimit; joint.angularXDrive = part.attachJoint.Joint.angularXDrive; joint.angularYZDrive = part.attachJoint.Joint.angularYZDrive; joint.xDrive = part.attachJoint.Joint.xDrive; joint.yDrive = part.attachJoint.Joint.yDrive; joint.zDrive = part.attachJoint.Joint.zDrive; jointRigidBody = joint.GetComponent <Rigidbody>(); // Set anchor position joint.anchor = jointRigidBody.transform.InverseTransformPoint(joint.connectedBody.transform.position); joint.connectedAnchor = Vector3.zero; // Set correct axis joint.axis = jointRigidBody.transform.InverseTransformDirection(joint.connectedBody.transform.right); //x axis joint.secondaryAxis = jointRigidBody.transform.InverseTransformDirection(joint.connectedBody.transform.up); //y axis joint.enableCollision = false; /* * if (translateJoint) * { * //we need to get joint's translation along the translate axis * var right = joint.axis; //x axis * var up = joint.secondaryAxis; //y axis * var forward = Vector3.Cross(joint.axis, joint.secondaryAxis).normalized; //z axis * var r = Quaternion.LookRotation(forward, up); * Vector3 f = r * (-translateAxis); * * startPosition = Vector3.Dot(jointRigidBody.transform.InverseTransformPoint(joint.connectedBody.transform.position) - joint.anchor, f); * * Logger.Log(servoName + ": right = " + right + ", forward = " + forward + ", up = " + up + ", trAxis=" + translateAxis + ", f=" + f + ", startposition=" + startPosition, Logger.Level.Debug); * * joint.xMotion = ConfigurableJointMotion.Free; * joint.yMotion = ConfigurableJointMotion.Free; * joint.zMotion = ConfigurableJointMotion.Free; * } * * if (rotateJoint) * { * startPosition = to180(AngleSigned(jointRigidBody.transform.up, joint.connectedBody.transform.up, joint.connectedBody.transform.right)); * * joint.rotationDriveMode = RotationDriveMode.XYAndZ; * joint.angularXMotion = ConfigurableJointMotion.Free; * joint.angularYMotion = ConfigurableJointMotion.Free; * joint.angularZMotion = ConfigurableJointMotion.Free; * * if (jointSpring > 0) * { * if (rotateAxis == Vector3.right || rotateAxis == Vector3.left) * { * JointDrive drv = joint.angularXDrive; * drv.positionSpring = jointSpring; * drv.positionDamper = jointDamping; * joint.angularXDrive = drv; * joint.angularYMotion = ConfigurableJointMotion.Locked; * joint.angularZMotion = ConfigurableJointMotion.Locked; * } * else * { * JointDrive drv = joint.angularYZDrive; * drv.positionSpring = jointSpring; * drv.positionDamper = jointDamping; * joint.angularYZDrive = drv; * * joint.angularXMotion = ConfigurableJointMotion.Locked; * joint.angularZMotion = ConfigurableJointMotion.Locked; * } * } * } */ // Reset default joint drives var resetDrv = new JointDrive { positionSpring = 0, positionDamper = 0, maximumForce = 0 }; part.attachJoint.Joint.angularXDrive = resetDrv; part.attachJoint.Joint.angularYZDrive = resetDrv; part.attachJoint.Joint.xDrive = resetDrv; part.attachJoint.Joint.yDrive = resetDrv; part.attachJoint.Joint.zDrive = resetDrv; part.attachJoint.Joint.enableCollision = false; }
void SetGrapple(GameObject targ, int grappleType) { if (targ == null) { return; } if (curTarget != null || curGrapple != null) { FreeGrapple(); } swinging = true; curTarget = targ; controller.SetActions(true, false); Rigidbody targRigid = curTarget.GetComponent <Rigidbody> (); if (targRigid != null && curTarget != null) { if (grappleType == 0) { Vector3 heading = (curTarget.transform.position + (Vector3.down * 2) - tr.position) / 2; int dist = (int)heading.magnitude / 2; Vector3 normal = heading / dist; Vector3 startPos = normal; rigid.AddForce((normal + Vector3.up) * 500, ForceMode.Impulse); GameObject wrapper = new GameObject(); wrapper.name = "Chain Wrapper"; wrapper.transform.position = anchorStart.position + Vector3.forward + (normal * 2); Debug.LogWarning(wrapper.transform.position); ConfigurableJoint cachedJoint = null; for (int i = 0; i < dist - 1; i++) { ConfigurableJoint joint = SpawnHinge(); joint.transform.parent = wrapper.transform; joint.transform.localPosition = startPos; joint.transform.rotation = Quaternion.LookRotation(normal); if (cachedJoint != null) { Rigidbody rig = joint.GetComponent <Rigidbody> (); cachedJoint.connectedBody = rig; FixedJoint fixedJ = cachedJoint.GetComponent <FixedJoint> (); if (fixedJ == null) { fixedJ = cachedJoint.gameObject.AddComponent <FixedJoint> (); } fixedJ.connectedBody = rig; } else //start link { Debug.LogWarning("Boop"); if (playerAnchor == null) { playerAnchor = gameObject.AddComponent <FixedJoint> (); } playerAnchor.connectedBody = joint.GetComponent <Rigidbody> (); playerAnchor.connectedBody.mass = 100; } cachedJoint = joint; startPos += normal * 2; Debug.LogWarning(startPos); } cachedJoint.connectedBody = targRigid; FixedJoint fixedJoint = cachedJoint.GetComponent <FixedJoint> (); if (fixedJoint == null) { fixedJoint = cachedJoint.gameObject.AddComponent <FixedJoint> (); } fixedJoint.connectedBody = targRigid; curGrapple = wrapper.transform; } else if (grappleType == 1) { SpringJoint target_spring = targRigid.gameObject.AddComponent <SpringJoint> (); target_spring.connectedBody = rigid; target_spring.autoConfigureConnectedAnchor = false; target_spring.connectedAnchor = targRigid.transform.position; target_spring.maxDistance = 50; target_spring.minDistance = 5; } } // Debug.LogError ("Coolio"); }
public void Initiate(MuscleLite[] colleagues) { name = joint.name; transform = joint.transform; rigidbody = joint.GetComponent <Rigidbody>(); if (joint.connectedBody != null) { for (int i = 0; i < colleagues.Length; i++) { if (colleagues[i].joint.GetComponent <Rigidbody>() == joint.connectedBody) { connectedBodyTarget = colleagues[i].target; } if (colleagues[i] == this) { index = i; } } joint.autoConfigureConnectedAnchor = false; connectedBodyTransform = joint.connectedBody.transform; directTargetParent = target.parent == connectedBodyTarget; } targetParent = connectedBodyTarget != null ? connectedBodyTarget : target.parent; toParentSpace = Quaternion.Inverse(targetParentRotation) * parentRotation; localRotationConvert = Quaternion.Inverse(targetLocalRotation) * localRotation; // Joint space Vector3 forward = Vector3.Cross(joint.axis, joint.secondaryAxis).normalized; Vector3 up = Vector3.Cross(forward, joint.axis).normalized; defaultLocalRotation = localRotation; Quaternion toJointSpace = Quaternion.LookRotation(forward, up); toJointSpaceInverse = Quaternion.Inverse(toJointSpace); toJointSpaceDefault = defaultLocalRotation * toJointSpace; // Set joint params joint.rotationDriveMode = RotationDriveMode.Slerp; joint.configuredInWorldSpace = false; // Fix target Transforms defaultTargetLocalPosition = target.localPosition; defaultTargetLocalRotation = target.localRotation; targetAnimatedCenterOfMass = V3Tools.TransformPointUnscaled(target, rigidbody.centerOfMass); // Resetting if (joint.connectedBody == null) { defaultPosition = transform.localPosition; defaultRotation = transform.localRotation; } else { defaultPosition = joint.connectedBody.transform.InverseTransformPoint(transform.position); defaultRotation = Quaternion.Inverse(joint.connectedBody.transform.rotation) * transform.rotation; } rigidbody.isKinematic = false; Read(); initiated = true; }
Vector3 jointRotation(ConfigurableJoint joint) { Quaternion jointBasis = Quaternion.LookRotation(joint.secondaryAxis, Vector3.Cross(joint.axis, joint.secondaryAxis)); Quaternion jointBasisInverse = Quaternion.Inverse(jointBasis); var rotation = (jointBasisInverse * Quaternion.Inverse(joint.connectedBody.rotation) * joint.GetComponent <Rigidbody>().transform.rotation *jointBasis).eulerAngles; return(new Vector3(to180(rotation.x), to180(rotation.z), to180(rotation.y))); }
/* * This region contains the various interactions that can be performed once tongue has hit a target. * The type of interaction performed is determined by checking the targetType of the target object. * * FUNCTIONS IN THIS REGION: * Tongue Swing * Tongue Eat * Tongue Grab * Terminate Grab */ IEnumerator TongueSwing(SCR_TongueTarget target) { // Stop targeting currentTarget = null; tongueState = TongueState.Attached; ConfigurableJoint targetJoint = target.GetComponentInChildren <ConfigurableJoint>(); Rigidbody targetJointRb = targetJoint.GetComponent <Rigidbody>(); // Turn player towards target Quaternion targetRotation = Quaternion.LookRotation(target.transform.position - tongueTargetAnchor.position); transform.rotation = targetRotation; targetJoint.transform.rotation = targetRotation; // Tell target that player is swinging on it target.isBeingSwung = true; // Connect player to target joint targetJoint.connectedBody = rb; // Set target distance while swinging (promote to variable later) targetJoint.targetPosition = new Vector3(0, 0, Vector3.Distance(tongueTargetAnchor.position, targetJoint.transform.position) - variables.swingDistance); // Set initial swing velocity. Change this in future to convert linear velocity to angular //rb.velocity = rb.velocity.normalized * 7; rb.velocity = new Vector3(rb.velocity.x, 0, rb.velocity.z); // Deactivate normal movement movement.overrideNormalMovement = true; movement.overrideJump = true; movement.overrideRotation = true; movement.canMidairJump = false; rb.constraints &= ~RigidbodyConstraints.FreezeRotationX; rb.constraints &= ~RigidbodyConstraints.FreezeRotationY; rb.constraints &= ~RigidbodyConstraints.FreezeRotationZ; float swingTime = 0; while ((holdingTongueButton || swingTime < 0.2f) && target != null) { swingTime += Time.deltaTime; // Add some forward rotation at the beginning of swing if (target.startWithVelocity && swingTime < 0.1f) { targetJointRb.AddRelativeTorque(-400 * Time.deltaTime, 0, 0, ForceMode.Impulse); } tongueCollider.position = target.transform.position; yield return(null); } if (target != null) { // Break connection to joint targetJoint.connectedBody = null; // Tell target that player stopped swinging on it target.isBeingSwung = false; } // Set velocity on swing end. Change this in future to convert linear velocity to angular //rb.velocity = rb.velocity.normalized * targetJointRb.angularVelocity.magnitude * 2; movement.externalVelocity.velocity = rb.velocity; if (movement.externalVelocity.velocity.y < 1) { movement.externalVelocity.velocity = new Vector3(movement.externalVelocity.velocity.x, 1, movement.externalVelocity.velocity.z); } // Re-activate normal movement movement.overrideNormalMovement = false; movement.overrideJump = false; movement.overrideRotation = false; rb.constraints = RigidbodyConstraints.FreezeRotation; // Refresh midair jump after swinging movement.canMidairJump = true; StartCoroutine(TongueRetract(tongueCollider.transform.position, 1)); }