// Update is called once per frame void Update() { if (Target != null) { Vector3 LookVector = (Target.transform.position - this.transform.position); this.transform.rotation = Quaternion.Slerp(this.transform.rotation, Quaternion.LookRotation(LookVector), 360); if (StarShipOwner != null) { Radius = TractorRadius + _ohm.ShipRadius + StarShipOwner.GetComponent <HealthModule>().ShipRadius; } else { Radius = TractorRadius + _ohm.ShipRadius; } } if (Target != null) { if (Vector3.Distance(gameObject.transform.position, Target.transform.position) > Radius) { Use = false; Target = null; if (StarShipOwner != null) { StarShipOwner.GetComponent <HealthModule>().Catched = false; } StarShipOwner = null; } } if (Use) { if (Target != null) { if (curColorAmount < 1) { curColorAmount += Time.deltaTime; } if (!WorkingEffect.isPlaying) { if (!OnActive) { OnEffect.Play(); OnActive = true; } WorkingEffect.Play(); } _lbObject.GenerateBeam(); _lbObject.gameObject.GetComponent <MeshRenderer>().material.SetColor("_Color", Color.Lerp(OffColor, OnColor, curColorAmount)); _lbObject.gameObject.SetActive(true); _lbObject.Length = Vector3.Distance(gameObject.transform.position, StarShipOwner.transform.position) + StarShipOwner.GetComponent <HealthModule>().ShipRadius; _lbObject.RadiusBottom = StarShipOwner.GetComponent <HealthModule>().ShipRadius *2; if (StarShipOwner != null) { if (Vector3.Distance(gameObject.transform.position, StarShipOwner.transform.position) < Radius - 7) { StarShipOwner.GetComponent <Rigidbody>().AddForce((Target.GetComponent <Rigidbody>().velocity *(Tspeed + 2) * Target.GetComponent <Rigidbody>().mass) * -1); } if (Vector3.Distance(gameObject.transform.position, StarShipOwner.transform.position) > Radius - 7) { Vector3 LookVector = (StarShipOwner.transform.position - this.transform.position); StarShipOwner.GetComponent <Rigidbody>().AddForce(((LookVector * (Tspeed + 1)) * (Target.GetComponent <Rigidbody>().mass / 2)) * -1); } if (Vector3.Distance(gameObject.transform.position, StarShipOwner.transform.position) > Radius - 5) { Vector3 LookVector = (StarShipOwner.transform.position - this.transform.position); StarShipOwner.GetComponent <Rigidbody>().AddForce(((LookVector * (Tspeed + 1)) * (Target.GetComponent <Rigidbody>().mass)) * -1); } StarShipOwner.GetComponent <HealthModule>().Catched = true; } else { if (Vector3.Distance(gameObject.transform.position, Target.transform.position) < Radius - 7) { Target.GetComponent <Rigidbody>().AddForce((Target.GetComponent <Rigidbody>().velocity *(Tspeed + 2) * Target.GetComponent <Rigidbody>().mass) * -1); } if (Vector3.Distance(gameObject.transform.position, Target.transform.position) > Radius - 7) { Vector3 LookVector = (Target.transform.position - this.transform.position); Target.GetComponent <Rigidbody>().AddForce(((LookVector * (Tspeed + 1)) * (Target.GetComponent <Rigidbody>().mass / 2)) * -1); } if (Vector3.Distance(gameObject.transform.position, Target.transform.position) > Radius - 5) { Vector3 LookVector = (Target.transform.position - this.transform.position); Target.GetComponent <Rigidbody>().AddForce(((LookVector * (Tspeed + 1)) * (Target.GetComponent <Rigidbody>().mass)) * -1); } } } } else { if (curColorAmount > 0) { curColorAmount -= Time.deltaTime; } if (WorkingEffect.isPlaying) { OffEffect.Play(); WorkingEffect.Stop(); } OnActive = false; Target = null; _lbObject.gameObject.GetComponent <MeshRenderer>().material.SetColor("_Color", Color.Lerp(OffColor, OnColor, curColorAmount)); if (_lbObject.gameObject.GetComponent <MeshRenderer>().material.GetColor("_Color") == OffColor) { _lbObject.gameObject.SetActive(false); } } }
static void ModifyMesh(Lightbeam lightBeam) { lightBeam.Settings = GetLigthbeamSettings(lightBeam); lightBeam.IsModifyingMesh = true; lightBeam.GenerateBeam(); }
public override void OnInspectorGUI() { EditorGUIUtility.LookLikeInspector(); Lightbeam lightBeam = (target as Lightbeam); if (lightBeam.IsModifyingMesh == true && IsLightBeamMesh() == false) { Debug.LogWarning("Escaped lightbeam modification mode. No valid lightbeam mesh was found."); lightBeam.IsModifyingMesh = false; } if (lightBeam.IsModifyingMesh == false) { EditorGUILayout.BeginHorizontal(); if (IsLightBeamMesh()) { if (GUILayout.Button(new GUIContent("Modify", "Modify the lightbeam mesh."))) { Undo.RegisterUndo(lightBeam, "Modify light beam"); ModifyMesh(lightBeam); EditorUtility.SetDirty(lightBeam); } } else { GUI.enabled = false; if (GUILayout.Button(new GUIContent("Modify", "Missing a valid lightbeam mesh."))) { } GUI.enabled = true; } if (GUILayout.Button(new GUIContent("New Lightbeam Mesh", "Create a new lightbeam mesh for this object."))) { int result = EditorUtility.DisplayDialogComplex("Duplicate material", "Do you want to duplicate the material?\nIf you don't duplicate the same material will be used.", "Yes", "No", "Cancel"); if (result == 0) // ok { Undo.RegisterSceneUndo("New Lightbeam Mesh"); SaveNewAsset(lightBeam, true); ModifyMesh(lightBeam); EditorUtility.SetDirty(lightBeam); } if (result == 1) // no { Undo.RegisterSceneUndo("New Lightbeam Mesh"); SaveNewAsset(lightBeam, false); ModifyMesh(lightBeam); EditorUtility.SetDirty(lightBeam); } } EditorGUILayout.EndHorizontal(); } else { EditorGUILayout.BeginVertical(); string RadiusTopTooltip = "The top radius of the lightbeam."; string RadiusBottomTooltip = "The bottom radius of the lightbeam."; string LengthTooltip = "Length of the lightbeam."; string SubdivisionsTooltip = "Horizontal Subdivisions\n\nIncreasing this will smoothen the lightbeam by adding vertices."; string SubdivisionsHeightTooltip = "Vertical Subdivisions\n\nIncreasing this will smoothen the lightbeam by adding vertices."; lightBeam.RadiusTop = EditorGUILayout.FloatField(new GUIContent("Radius Top", RadiusTopTooltip), lightBeam.RadiusTop); lightBeam.RadiusBottom = EditorGUILayout.FloatField(new GUIContent("Radius Bottom", RadiusBottomTooltip), lightBeam.RadiusBottom); lightBeam.Length = EditorGUILayout.FloatField(new GUIContent("Length", LengthTooltip), lightBeam.Length); lightBeam.Subdivisions = EditorGUILayout.IntSlider(new GUIContent("Subdivisions", SubdivisionsTooltip), lightBeam.Subdivisions, 3, 50); lightBeam.SubdivisionsHeight = EditorGUILayout.IntSlider(new GUIContent("Subdivisions Height", SubdivisionsHeightTooltip), lightBeam.SubdivisionsHeight, 1, 10); if (lightBeam.RadiusTop <= 0.01f) { lightBeam.RadiusTop = 0.01f; } if (lightBeam.RadiusBottom <= 0.01f) { lightBeam.RadiusBottom = 0.01f; } EditorGUILayout.BeginHorizontal(); if (GUILayout.Button(new GUIContent("Done", "Finish modifying the lightbeam mesh."))) { Undo.RegisterUndo(lightBeam, "Finished Modifying Lightbeam Mesh"); lightBeam.IsModifyingMesh = false; EditorUtility.SetDirty(lightBeam); } EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); } if (GUI.changed) { Undo.SetSnapshotTarget(lightBeam.Settings, "Modify Lightbeam Mesh"); Undo.CreateSnapshot(); Undo.RegisterSnapshot(); lightBeam.GenerateBeam(); EditorUtility.SetDirty(lightBeam); } }
public void OnSceneGUI() { Lightbeam lightBeam = (target as Lightbeam); if (lightBeam.IsModifyingMesh == true && IsLightBeamMesh() == false) { Debug.LogWarning("Exited lightbeam modification mode. No valid lightbeam mesh was found."); lightBeam.IsModifyingMesh = false; } if (Event.current.type == EventType.ValidateCommand) { if (Event.current.commandName == "UndoRedoPerformed") { if (IsLightBeamMesh()) { lightBeam.GenerateBeam(); } } } if (lightBeam.IsModifyingMesh) { // Register the undos when we press the Mouse button. if (Event.current.type == EventType.MouseDown && Event.current.button == 0) { Undo.SetSnapshotTarget(lightBeam.Settings, "Modify Lightbeam Mesh"); Undo.CreateSnapshot(); Undo.RegisterSnapshot(); Undo.ClearSnapshotTarget(); } // red radius circles Handles.matrix = lightBeam.transform.localToWorldMatrix; Handles.color = Color.black; Handles.DrawWireDisc(new Vector3(0, -lightBeam.Length, 0), Vector3.up, lightBeam.RadiusBottom); Handles.DrawWireDisc(Vector3.zero, Vector3.up, lightBeam.RadiusTop); // handle positions and sizes Vector3 rTopPos = new Vector3(-lightBeam.RadiusTop, 0, 0); Vector3 rBottomPos = new Vector3(-lightBeam.RadiusBottom, -lightBeam.Length, 0); Vector3 lengthPos = new Vector3(0, -lightBeam.Length, 0); float rTopSize = HandleUtility.GetHandleSize(lightBeam.transform.TransformDirection(rTopPos)); float rBottomSize = HandleUtility.GetHandleSize(lightBeam.transform.TransformDirection(rBottomPos)); float lengthSize = HandleUtility.GetHandleSize(lightBeam.transform.TransformDirection(lengthPos)); // yellow radius handles Handles.color = Color.yellow; lightBeam.RadiusTop = Handles.ScaleValueHandle(lightBeam.RadiusTop, rTopPos, Quaternion.identity, rTopSize, Handles.CylinderCap, 2); lightBeam.RadiusBottom = Handles.ScaleValueHandle(lightBeam.RadiusBottom, rBottomPos, Quaternion.identity, rBottomSize, Handles.CylinderCap, 2); lightBeam.Length = Handles.ScaleValueHandle(lightBeam.Length, lengthPos, Quaternion.Euler(Vector3.up), lengthSize, Handles.CubeCap, 0); if (lightBeam.RadiusTop <= 0.01f) { lightBeam.RadiusTop = 0.01f; } if (lightBeam.RadiusBottom <= 0.01f) { lightBeam.RadiusBottom = 0.01f; } if (GUI.changed) { lightBeam.GenerateBeam(); EditorUtility.SetDirty(lightBeam); } } }