private void Generate() { UOUtility.DestroyChildren(Generated); wayPoints.Clear(); float localSpacing = 0; Joint joint = null; for (int i = 0; i < segmentCount; i++) { var seg = UOUtility.Instantiate(segmentPrefab, Generated.transform); seg.transform.Translate(0, 0, localSpacing); var segRB = seg.GetComponent <Rigidbody>(); // we fix the first segment so that the vine won't fall if (i == 0) { firstSegment = seg; segRB.constraints = RigidbodyConstraints.FreezePosition; } // we attach the rigidbody to the joint of the previous segment if (joint != null) { joint.connectedBody = segRB; } joint = seg.GetComponent <Joint>(); // we save segments as way points for the spline deformation. wayPoints.Add(seg); localSpacing += segmentSpacing; } UOUtility.Destroy(joint); }
private void GeneratePhysicsGOs() { UOUtility.DestroyChildren(GeneratedPhysicsGO); wayPoints.Clear(); float localSpacing = 0; Joint joint = null; for (int i = 0; i < segmentCount; i++) { GameObject seg = UOUtility.Instantiate(segmentPrefab, GeneratedPhysicsGO.transform); seg.transform.Translate(0, 0, localSpacing); seg.tag = "Vine"; Rigidbody segRB = seg.GetComponent <Rigidbody>(); // we fix the first segment so that the vine won't fall if (i == 0) { segRB.constraints = RigidbodyConstraints.FreezePosition; seg.GetComponent <Collider>().enabled = false; //disable the collider on the first/center of the plant. _weed.vinesToPickBeforeDestorying.Enqueue(seg.transform); //store the first physics segment so the player can grab it with IK } else if (i == Mathf.Ceil(segmentCount / 2)) { //spawn object GameObject plantBlock = new GameObject("Generated Plant Block Collider") { layer = 11 }; plantBlock.transform.position = seg.transform.position; plantBlock.transform.SetParent(seg.transform); SphereCollider plantBlockCollider = plantBlock.AddComponent <SphereCollider>(); plantBlockCollider.radius = i * segmentSpacing; } else if (i + 1 == segmentCount) { // last segment deals damage to anything that has health healthDecrementer = seg.AddComponent <HealthDecrementer>(); healthDecrementer.damageAmount = damageAmount; healthDecrementer.damageRateInSeconds = damageRateInSeconds; healthDecrementer.damagedEnabled = false; } // we attach the rigidbody to the joint of the previous segment if (joint != null) { joint.connectedBody = segRB; } joint = seg.GetComponent <Joint>(); // we save segments as way points for the spline deformation. wayPoints.Add(seg); localSpacing += segmentSpacing; } UOUtility.Destroy(joint); }
private void GeneratePhysicsGOs() { UOUtility.DestroyChildren(GeneratedPhysicsGO); wayPoints.Clear(); float localSpacing = 0; Joint joint = null; for (int i = 0; i < segmentCount; i++) { GameObject seg = UOUtility.Instantiate(segmentPrefab, GeneratedPhysicsGO.transform); seg.transform.Translate(0, 0, localSpacing); Rigidbody segRB = seg.GetComponent <Rigidbody>(); // we fix the first segment so that the vine won't fall if (i == 0) { firstSegment = seg; segRB.constraints = RigidbodyConstraints.FreezePosition; seg.GetComponent <Collider>().enabled = false; //disable the collider on the first/center of the plant. } else if (i == Mathf.Ceil(segmentCount / 2)) { //spawn object GameObject plantBlock = new GameObject("Generated Plant Block Collider") { layer = 11 }; plantBlock.transform.position = seg.transform.position; plantBlock.transform.SetParent(seg.transform); SphereCollider plantBlockCollider = plantBlock.AddComponent <SphereCollider>(); plantBlockCollider.radius = i * segmentSpacing; } // we attach the rigidbody to the joint of the previous segment if (joint != null) { joint.connectedBody = segRB; } joint = seg.GetComponent <Joint>(); // we save segments as way points for the spline deformation. wayPoints.Add(seg); localSpacing += segmentSpacing; } UOUtility.Destroy(joint); }
/// <summary> /// Instantiate the prefabs here /// </summary> public void Sow() { UOUtility.DestroyChildren(generated); UnityEngine.Random.InitState(randomSeed); if (spacing + spacingRange <= 0 || prefab == null) { return; } float distance = 0; while (distance <= spline.Length) { CurveSample sample = spline.GetSampleAtDistance(distance); GameObject go; go = Instantiate(prefab, generated.transform); go.transform.localRotation = Quaternion.identity; go.transform.localPosition = Vector3.zero; go.transform.localScale = Vector3.one; // move along spline, according to spacing + random go.transform.localPosition = sample.location; // apply scale + random float rangedScale = scale + UnityEngine.Random.Range(0, scaleRange); go.transform.localScale = new Vector3(rangedScale, rangedScale, rangedScale); // rotate with random yaw if (isRandomYaw) { go.transform.Rotate(0, 0, UnityEngine.Random.Range(-180, 180)); } else { go.transform.rotation = sample.Rotation; } // move orthogonaly to the spline, according to offset + random var binormal = (Quaternion.LookRotation(sample.tangent, sample.up) * Vector3.right).normalized; var localOffset = offset + UnityEngine.Random.Range(0, offsetRange * Math.Sign(offset)); localOffset *= sample.scale.x; binormal *= localOffset; go.transform.position += binormal; distance += spacing + UnityEngine.Random.Range(0, spacingRange); } }
void DuplicateOP() { if (m_Spline != null & m_Prefab != null && m_Generated != null) { UOUtility.DestroyChildren(m_Generated); UnityEngine.Random.InitState(m_RandomSeed); if (m_Spacing + m_SpacingRange <= 0 || m_Prefab == null) { return; } float distance = 0; while (distance <= m_Spline.Length) { CurveSample sample = m_Spline.GetSampleAtDistance(distance); GameObject go; go = Instantiate(m_Prefab, m_Generated.transform); go.transform.localRotation = Quaternion.identity; go.transform.localPosition = Vector3.zero; go.transform.localScale = Vector3.one; // move along spline, according to spacing + random go.transform.localPosition = sample.location; // apply scale + random float rangedScale = m_Scale + UnityEngine.Random.Range(0, m_ScaleRange); go.transform.localScale = new Vector3(rangedScale, rangedScale, rangedScale); switch (m_RotationMode) { case (RotationMode.FollowSplineNormal): go.transform.rotation = sample.Rotation; break; case (RotationMode.CustomRotation): go.transform.Rotate(m_Rotation); break; case (RotationMode.Combined): go.transform.rotation = sample.Rotation; go.transform.rotation *= Quaternion.Euler(m_Rotation); break; } // // rotate with random yaw if (m_RandomizeRotation) { float RandomRange = UnityEngine.Random.Range(m_MinRange, m_MaxRange); go.transform.rotation *= Quaternion.Euler(RandomRange, RandomRange, RandomRange); } // move orthogonaly to the spline, according to offset + random Vector3 binormal = Vector3.zero; float localOffset = 1.0f; switch (m_OffsetAxiz) { case OffsetAxis.X: binormal = (Quaternion.LookRotation(sample.tangent, sample.up) * Vector3.right).normalized; localOffset = m_Offset + UnityEngine.Random.Range(0, m_OffsetRange * Math.Sign(m_Offset)); localOffset *= sample.scale.x; break; case OffsetAxis.Y: binormal = (Quaternion.LookRotation(sample.tangent, sample.up) * Vector3.up).normalized; localOffset = m_Offset + UnityEngine.Random.Range(m_OffsetRange * Math.Sign(m_Offset), 0); localOffset *= sample.scale.y; break; } binormal *= localOffset; go.transform.position += binormal; distance += m_Spacing + UnityEngine.Random.Range(0, m_SpacingRange); } } }