private void UpdateSnap() { if (snapSurface != null && (snapPosition == true || snapRotation != RotationSnap.None)) { var point = transform.position; var centre = snapSurface.transform.position; var position = SurfacePositionAtPoint(centre, point); if (snapPosition == true) { SGT_Helper.SetPosition(transform, position); } if (snapRotation == RotationSnap.AlignToCentre) { var finalRot = Quaternion.FromToRotation(transform.up, (position - centre).normalized) * transform.rotation; SGT_Helper.SetRotation(transform, finalRot); } if (snapRotation == RotationSnap.AlignToNormal) { var position2 = SurfacePositionAtPoint(centre, point + transform.forward * snapRotationScanDistance); var position3 = SurfacePositionAtPoint(centre, point + transform.right * snapRotationScanDistance); var finalRot = Quaternion.FromToRotation(transform.up, Vector3.Cross(position2 - position, position3 - position)) * transform.rotation; SGT_Helper.SetRotation(transform, finalRot); } } }
public void UpdateFollow() { if (followTarget != null) { if (followPosition == true) { SGT_Helper.SetPosition(transform, followTarget.position * followPositionScale); } if (followRotation == true) { SGT_Helper.SetRotation(transform, followTarget.rotation); } } }
public void LateUpdate() { if (thrusterObserver == null) { thrusterObserver = SGT_Helper.FindCamera(); } if (thrusterFlameGameObject == null) { thrusterFlameGameObject = SGT_Helper.CreateGameObject("Flame", gameObject); } if (thrusterFlareGameObject == null) { thrusterFlareGameObject = SGT_Helper.CreateGameObject("Flare", gameObject); } SGT_Helper.SetParent(thrusterFlameGameObject, gameObject); SGT_Helper.SetLayer(thrusterFlameGameObject, gameObject.layer); SGT_Helper.SetTag(thrusterFlameGameObject, gameObject.tag); SGT_Helper.SetParent(thrusterFlareGameObject, gameObject); SGT_Helper.SetLayer(thrusterFlareGameObject, gameObject.layer); SGT_Helper.SetTag(thrusterFlareGameObject, gameObject.tag); if (thrusterPhysics == true && thrusterPhysicsRigidbody == null) { thrusterPhysicsRigidbody = SGT_Helper.GetComponentUpwards <Rigidbody>(gameObject); } var observerPosition = SGT_Helper.GetPosition(thrusterObserver); if (Application.isPlaying == true) { currentThrusterThrottle = Mathf.MoveTowards(currentThrusterThrottle, targetThrusterThrottle, thrusterTweenSpeed * Time.deltaTime); } else { currentThrusterThrottle = targetThrusterThrottle; } if (thrusterFlame == true) { if (thrusterFlameMesh == null) { thrusterFlameMesh = new SGT_Mesh(); } // Offset flame SGT_Helper.SetLocalPosition(thrusterFlameGameObject.transform, thrusterFlameOffset); var finalFlameScale = thrusterFlameScale + thrusterFlameScaleChange * currentThrusterThrottle; // Hide/show flame if (finalFlameScale == Vector3.zero) { thrusterFlameMesh.MeshRendererEnabled = false; } else { if (Application.isPlaying == true) { finalFlameScale *= Random.Range(1.0f - thrusterFlameScaleFlicker, 1.0f); } thrusterFlameMesh.MeshRendererEnabled = true; SGT_Helper.SetLocalScale(thrusterFlameGameObject.transform, finalFlameScale); // Roll flame to observer var pointDir = transform.InverseTransformPoint(observerPosition); var roll = Mathf.Atan2(pointDir.y, pointDir.x) * Mathf.Rad2Deg; SGT_Helper.SetRotation(thrusterFlameGameObject.transform, transform.rotation * Quaternion.Euler(0.0f, 0.0f, roll)); } thrusterFlameMesh.GameObject = thrusterFlameGameObject; thrusterFlameMesh.HasMeshRenderer = true; thrusterFlameMesh.Update(); } else { if (thrusterFlameMesh != null) { thrusterFlameMesh = thrusterFlameMesh.Clear(); } } if (thrusterFlare == true) { if (thrusterFlareMesh == null) { thrusterFlareMesh = new SGT_Mesh(); } // Offset flare SGT_Helper.SetLocalPosition(thrusterFlareGameObject.transform, thrusterFlareOffset); // Flare visible? var a = thrusterFlareGameObject.transform.position; var b = observerPosition; var direction = (b - a).normalized; var distance = (b - a).magnitude; var targetFlareSize = 0.0f; // If the ray hits something, then hide the flare if (Physics.Raycast(a, direction, distance, thrusterFlareRaycastMask) == true) { targetFlareSize = 0.0f; } else { targetFlareSize = 1.0f; } // Point flare at observer if (thrusterObserver != null) { SGT_Helper.SetRotation(thrusterFlareGameObject.transform, thrusterObserver.transform.rotation); } // Fade flare in/out based on raycast if (Application.isPlaying == true) { currentThrusterFlareScale = Mathf.MoveTowards(currentThrusterFlareScale, targetFlareSize, thrusterFlareScaleTweenSpeed * Time.deltaTime); } else { currentThrusterFlareScale = targetFlareSize; } var finalFlareScale = currentThrusterFlareScale * (thrusterFlareScale + thrusterFlareScaleChange * currentThrusterThrottle); // Hide/show flare if (finalFlareScale == Vector3.zero) { thrusterFlareMesh.MeshRendererEnabled = false; } else { if (Application.isPlaying == true) { finalFlareScale *= Random.Range(1.0f - thrusterFlareScaleFlicker, 1.0f); } thrusterFlareMesh.MeshRendererEnabled = true; SGT_Helper.SetLocalScale(thrusterFlareGameObject.transform, finalFlareScale); } thrusterFlareMesh.GameObject = thrusterFlareGameObject; thrusterFlareMesh.HasMeshRenderer = true; thrusterFlareMesh.Update(); } else { if (thrusterFlareMesh != null) { thrusterFlareMesh = thrusterFlareMesh.Clear(); } } #if UNITY_EDITOR == true if (thrusterFlameMesh != null) { thrusterFlameMesh.HideInEditor(); } if (thrusterFlareMesh != null) { thrusterFlareMesh.HideInEditor(); } SGT_Helper.HideGameObject(thrusterFlameGameObject); SGT_Helper.HideGameObject(thrusterFlareGameObject); #endif }