public override void AssignColliders(Moveable_Drag draggable) { if (!UsesEndColliders) { base.AssignColliders(draggable); return; } if (draggable.maxCollider == null) { draggable.maxCollider = (Collider)Instantiate(Resources.Load(Resource.dragCollider, typeof(Collider))); } if (draggable.minCollider == null) { draggable.minCollider = (Collider)Instantiate(Resources.Load(Resource.dragCollider, typeof(Collider))); } draggable.maxCollider.transform.position = transform.position + (transform.up * maxDistance) + (transform.up * draggable.ColliderWidth); draggable.minCollider.transform.position = transform.position - (transform.up * draggable.ColliderWidth); draggable.minCollider.transform.up = transform.up; draggable.maxCollider.transform.up = -transform.up; base.AssignColliders(draggable); }
public override void ApplyDragForce(Vector3 force, Moveable_Drag draggable) { float dotProduct = 0f; if (rotationType == DragRotationType.Screw) { if (dragMustScrew) { draggable.UpdateScrewVector(); } dotProduct = Vector3.Dot(force, draggable._dragVector); } else { dotProduct = Vector3.Dot(force, transform.up); } // Calculate the amount of force along the tangent Vector3 tangentForce = transform.up * dotProduct; if (rotationType == DragRotationType.Screw) { if (dragMustScrew) { // Take radius into account tangentForce = (transform.up * dotProduct).normalized * force.magnitude; tangentForce /= Mathf.Sqrt((draggable.GetGrabPosition() - draggable.transform.position).magnitude) / 0.4f; } tangentForce /= Mathf.Sqrt(screwThread); } draggable.Rigidbody.AddForce(tangentForce, ForceMode.Force); }
public override void SnapToTrack(Moveable_Drag draggable, bool onStart) { Vector3 vec = draggable.transform.position - transform.position; float proportionAlong = Vector3.Dot(vec, transform.up) / maxDistance; proportionAlong = Mathf.Clamp01(proportionAlong); if (onStart) { if (rotationType != DragRotationType.None) { SetRotation(draggable, proportionAlong); } draggable.Rigidbody.velocity = draggable.Rigidbody.angularVelocity = Vector3.zero; } draggable.transform.position = transform.position + transform.up * proportionAlong * maxDistance; // Limit velocity to just along track Vector3 localVelocity = transform.InverseTransformDirection(draggable.Rigidbody.velocity); localVelocity.x = 0; localVelocity.z = 0; draggable.Rigidbody.velocity = transform.TransformDirection(localVelocity); }
/** * <summary>Corrects the position of an object so that it is placed along the track.</summary> * <param name = "draggable">The Moveable_Drag object to snap onto the track</param> * <param name = "onStart">Is True if the game has just begun (i.e. this function is being run for the first time)</param> */ public override void SnapToTrack(Moveable_Drag draggable, bool onStart) { Vector3 vec = draggable.transform.position - transform.position; float proportionAlong = Vector3.Dot(vec, transform.up) / maxDistance; if (onStart) { if (proportionAlong < 0f) { proportionAlong = 0f; } else if (proportionAlong > 1f) { proportionAlong = 1f; } if (rotationType != DragRotationType.None) { SetRotation(draggable, proportionAlong); } draggable._rigidbody.velocity = draggable._rigidbody.angularVelocity = Vector3.zero; } draggable.transform.position = transform.position + transform.up * proportionAlong * maxDistance; }
/** * <summary>Corrects the position of an object so that it is placed along the track.</summary> * <param name = "draggable">The Moveable_Drag object to snap onto the track</param> * <param name = "onStart">Is True if the game has just begun (i.e. this function is being run for the first time)</param> */ public override void SnapToTrack(Moveable_Drag draggable, bool onStart) { Vector3 LookAt = draggable.transform.position - transform.position; draggable.transform.position = transform.position + LookAt / (LookAt.magnitude / radius); if (onStart) { float proportionAlong = GetDecimalAlong(draggable); if (proportionAlong < 0f) { proportionAlong = 0f; } else if (proportionAlong > 1f) { proportionAlong = 1f; } draggable.transform.rotation = Quaternion.AngleAxis(proportionAlong * maxAngle, transform.forward) * transform.rotation; SetPositionAlong(proportionAlong, draggable); } else { draggable.transform.rotation = Quaternion.AngleAxis(draggable.trackValue * maxAngle, transform.forward) * transform.rotation; } }
override public void ShowGUI(List <ActionParameter> parameters) { dragParameterID = Action.ChooseParameterGUI("Drag object:", parameters, dragParameterID, ParameterType.GameObject); if (dragParameterID >= 0) { dragConstantID = 0; dragObject = null; } else { dragObject = (Moveable_Drag)EditorGUILayout.ObjectField("Drag object:", dragObject, typeof(Moveable_Drag), true); dragConstantID = FieldToID <Moveable_Drag> (dragObject, dragConstantID); dragObject = IDToField <Moveable_Drag> (dragObject, dragConstantID, false); if (dragObject != null && dragObject.dragMode != DragMode.LockToTrack) { EditorGUILayout.HelpBox("The chosen Drag object must be in 'Lock To Track' mode", MessageType.Warning); } } condition = (IntCondition)EditorGUILayout.EnumPopup("Condition:", condition); checkPositionParameterID = Action.ChooseParameterGUI("Position:", parameters, checkPositionParameterID, ParameterType.Float); if (checkPositionParameterID < 0) { checkPosition = EditorGUILayout.Slider("Position:", checkPosition, 0f, 1f); } if (condition == IntCondition.EqualTo || condition == IntCondition.NotEqualTo) { errorMargin = EditorGUILayout.Slider("Error margin:", errorMargin, 0f, 1f); } }
/** * <summary>Initialises two end colliders for an object that prevent it from moving beyond the track.</summary> * <param name = "draggable">The Moveable_Drag object to create colliders for</param> */ public override void AssignColliders(Moveable_Drag draggable) { if (draggable.maxCollider == null) { draggable.maxCollider = (Collider)Instantiate(Resources.Load("DragCollider", typeof(Collider))); } if (draggable.minCollider == null) { draggable.minCollider = (Collider)Instantiate(Resources.Load("DragCollider", typeof(Collider))); } if (maxAngle > 360f) { maxAngle = 360f; } float offsetAngle = Mathf.Asin(draggable.colliderRadius / radius) * Mathf.Rad2Deg; draggable.maxCollider.transform.position = startPosition; draggable.maxCollider.transform.up = -transform.up; draggable.maxCollider.transform.RotateAround(transform.position, transform.forward, maxAngle + offsetAngle); draggable.minCollider.transform.position = startPosition; draggable.minCollider.transform.up = transform.up; draggable.minCollider.transform.RotateAround(transform.position, transform.forward, -offsetAngle); base.AssignColliders(draggable); }
public void ProcessCollision(Collision collision, Moveable_Drag draggable) { if ((blockLayerMask.value & 1 << collision.gameObject.layer) != 0) { draggable.StopAutoMove(false); } }
/** * <summary>Connects a draggable object to the track defined in this data block</summary> * <param name="draggable">The draggable object to update</param> */ public void MakeConnection(Moveable_Drag draggable) { // First need to decide which snap point on the connectedTrack is closest to the draggable TrackSnapData winningSnap = null; float winningDistance = Mathf.Infinity; foreach (TrackSnapData trackSnapData in connectedTrack.allTrackSnapData) { if (!trackSnapData.IsEnabled) { continue; } Vector3 snapWorldPosition = trackSnapData.GetWorldPosition(connectedTrack); float sqrDist = (snapWorldPosition - draggable.Transform.position).sqrMagnitude; if (sqrDist < winningDistance) { winningDistance = sqrDist; winningSnap = trackSnapData; } } if (winningSnap != null) { draggable.SnapToTrack(connectedTrack, winningSnap.ID); } }
protected void DoSnapCheck(Moveable_Drag draggable) { if (doSnapping && !draggable.IsAutoMoving() && !draggable.IsHeld) { SnapToNearest(draggable); } }
public void Stop(DragTrack track, Moveable_Drag draggable, bool snapToTarget) { if (snapToTarget) { track.SetPositionAlong(targetValue, draggable); } }
public override void SnapToTrack (Moveable_Drag draggable, bool onStart) { Vector3 vec = draggable.transform.position - transform.position; float proportionAlong = Vector3.Dot (vec, transform.up) / maxDistance; if (onStart) { if (proportionAlong < 0f) { proportionAlong = 0f; } else if (proportionAlong > 1f) { proportionAlong = 1f; } if (rotationType != DragRotationType.None) { SetRotation (draggable, proportionAlong); } draggable._rigidbody.velocity = draggable._rigidbody.angularVelocity = Vector3.zero; } draggable.transform.position = transform.position + transform.up * proportionAlong * maxDistance; }
/** * <summary>Initialises two end colliders for an object that prevent it from moving beyond the track.</summary> * <param name = "draggable">The Moveable_Drag object to create colliders for</param> */ public override void AssignColliders(Moveable_Drag draggable) { if (draggable.maxCollider == null) { draggable.maxCollider = (Collider) Instantiate (Resources.Load ("DragCollider", typeof (Collider))); } if (draggable.minCollider == null) { draggable.minCollider = (Collider) Instantiate (Resources.Load ("DragCollider", typeof (Collider))); } if (maxAngle > 360f) { maxAngle = 360f; } float offsetAngle = Mathf.Asin (draggable.colliderRadius / radius) * Mathf.Rad2Deg; draggable.maxCollider.transform.position = startPosition; draggable.maxCollider.transform.up = -transform.up; draggable.maxCollider.transform.RotateAround (transform.position, transform.forward, maxAngle + offsetAngle); draggable.minCollider.transform.position = startPosition; draggable.minCollider.transform.up = transform.up; draggable.minCollider.transform.RotateAround (transform.position, transform.forward, -offsetAngle); base.AssignColliders (draggable); }
public override void AssignColliders(Moveable_Drag draggable) { if (!UsesEndColliders) { base.AssignColliders(draggable); return; } if (draggable.maxCollider == null) { draggable.maxCollider = (Collider)Instantiate(Resources.Load(Resource.dragCollider, typeof(Collider))); } if (draggable.minCollider == null) { draggable.minCollider = (Collider)Instantiate(Resources.Load(Resource.dragCollider, typeof(Collider))); } float offsetAngle = Mathf.Asin(draggable.ColliderWidth / radius) * Mathf.Rad2Deg; draggable.maxCollider.transform.position = startPosition; draggable.maxCollider.transform.up = -transform.up; draggable.maxCollider.transform.RotateAround(transform.position, transform.forward, maxAngle + offsetAngle); draggable.minCollider.transform.position = startPosition; draggable.minCollider.transform.up = transform.up; draggable.minCollider.transform.RotateAround(transform.position, transform.forward, -offsetAngle); base.AssignColliders(draggable); }
/** * <summary>Applies a force to an object connected to the track.</summary> * <param name = "force">The drag force vector input by the player</param> * <param name = "draggable">The Moveable_Drag object to apply the force to</param> */ public override void ApplyDragForce(Vector3 force, Moveable_Drag draggable) { float dotProduct = 0f; if (rotationType == DragRotationType.Screw) { if (dragMustScrew) { draggable.UpdateScrewVector (); } dotProduct = Vector3.Dot (force, draggable._dragVector); } else { dotProduct = Vector3.Dot (force, transform.up); } // Calculate the amount of force along the tangent Vector3 tangentForce = transform.up * dotProduct; if (rotationType == DragRotationType.Screw) { if (dragMustScrew) { // Take radius into account tangentForce = (transform.up * dotProduct).normalized * force.magnitude; tangentForce /= Mathf.Sqrt ((draggable.GetGrabPosition () - draggable.transform.position).magnitude) / 0.4f; } tangentForce /= Mathf.Sqrt (screwThread); } draggable._rigidbody.AddForce (tangentForce); }
public override void SetPositionAlong(float proportionAlong, Moveable_Drag draggable) { draggable.transform.position = transform.position; draggable.transform.rotation = Quaternion.AngleAxis(proportionAlong * MaxAngle, transform.forward) * transform.rotation; base.SetPositionAlong(proportionAlong, draggable); }
/** * <summary>Deserialises a string of data, and restores the GameObject to its previous state.</summary> * <param name = "stringData">The data, serialised as a string</param> */ public override void LoadData(string stringData) { MoveableData data = Serializer.LoadScriptData <MoveableData> (stringData); if (data == null) { loadedData = false; return; } SavePrevented = data.savePrevented; if (savePrevented) { return; } if (GetComponent <DragBase>()) { if (data.isOn) { GetComponent <DragBase>().TurnOn(); } else { GetComponent <DragBase>().TurnOff(); } } if (data.isOn) { gameObject.layer = LayerMask.NameToLayer(KickStarter.settingsManager.hotspotLayer); } else { gameObject.layer = LayerMask.NameToLayer(KickStarter.settingsManager.deactivatedLayer); } transform.position = new Vector3(data.LocX, data.LocY, data.LocZ); transform.eulerAngles = new Vector3(data.RotX, data.RotY, data.RotZ); transform.localScale = new Vector3(data.ScaleX, data.ScaleY, data.ScaleZ); if (GetComponent <Moveable_Drag>()) { Moveable_Drag moveable_Drag = GetComponent <Moveable_Drag>(); moveable_Drag.LetGo(true); if (moveable_Drag.dragMode == DragMode.LockToTrack && moveable_Drag.track != null) { moveable_Drag.trackValue = data.trackValue; moveable_Drag.revolutions = data.revolutions; moveable_Drag.StopAutoMove(); moveable_Drag.track.SetPositionAlong(data.trackValue, moveable_Drag); } } if (GetComponent <Moveable>()) { GetComponent <Moveable>().LoadData(data); } loadedData = true; }
public override void UpdateDraggable(Moveable_Drag draggable) { float oldValue = draggable.trackValue; draggable.Transform.position = Transform.position; draggable.trackValue = GetDecimalAlong(draggable); if (draggable.trackValue <= 0f || draggable.trackValue > 1f) { if (draggable.trackValue < 0f) { draggable.trackValue = 0f; } else if (draggable.trackValue > 1f) { draggable.trackValue = 1f; } if (draggable.UsesRigidbody) { draggable.Rigidbody.angularVelocity = Vector3.zero; } } SetPositionAlong(draggable.trackValue, draggable); if (Loops && limitRevolutions) { if (oldValue < 0.1f && draggable.trackValue > 0.9f) { draggable.revolutions--; } else if (oldValue > 0.9f && draggable.trackValue < 0.1f) { draggable.revolutions++; } if (draggable.revolutions < 0) { draggable.revolutions = 0; draggable.trackValue = 0f; SetPositionAlong(draggable.trackValue, draggable); draggable.Rigidbody.angularVelocity = Vector3.zero; } else if (draggable.revolutions > maxRevolutions - 1) { draggable.revolutions = maxRevolutions - 1; draggable.trackValue = 1f; SetPositionAlong(draggable.trackValue, draggable); draggable.Rigidbody.angularVelocity = Vector3.zero; } } if (!onlySnapOnPlayerRelease) { DoSnapCheck(draggable); } DoConnectionCheck(draggable); }
public override void AssignValues(List<ActionParameter> parameters) { dragObject = AssignFile <Moveable_Drag> (parameters, dragParameterID, dragConstantID, dragObject); positionAlong = AssignFloat (parameters, positionParameterID, positionAlong); positionAlong = Mathf.Max (0f, positionAlong); positionAlong = Mathf.Min (1f, positionAlong); }
/** * <summary>Applies a force to an object connected to the track.</summary> * <param name = "force">The drag force vector input by the player</param> * <param name = "draggable">The Moveable_Drag object to apply the force to</param> */ public override void ApplyDragForce(Vector3 force, Moveable_Drag draggable) { float dotProduct = Vector3.Dot (force, draggable.transform.up); // Calculate the amount of force along the tangent Vector3 tangentForce = draggable.transform.up * dotProduct; draggable._rigidbody.AddForce (tangentForce); }
override public void AssignValues(List <ActionParameter> parameters) { runtimeDragObject = AssignFile <Moveable_Drag> (parameters, dragParameterID, dragConstantID, dragObject); positionAlong = AssignFloat(parameters, positionParameterID, positionAlong); positionAlong = Mathf.Max(0f, positionAlong); positionAlong = Mathf.Min(1f, positionAlong); }
override public void AssignValues(List <ActionParameter> parameters) { dragObject = AssignFile <Moveable_Drag> (parameters, dragParameterID, dragConstantID, dragObject); checkPosition = AssignFloat(parameters, checkPositionParameterID, checkPosition); checkPosition = Mathf.Max(0f, checkPosition); checkPosition = Mathf.Min(1f, checkPosition); }
/** * <summary>Creates a new instance of the 'Object: Check track position' Action</summary> * <param name = "dragObject">The moveable object to query</param> * <param name = "trackRegionID">The ID of the track's region</param> * <returns>The generated Action</returns> */ public static ActionTrackCheck CreateNew (Moveable_Drag dragObject, int trackRegionID) { ActionTrackCheck newAction = (ActionTrackCheck) CreateInstance <ActionTrackCheck>(); newAction.method = TrackCheckMethod.WithinTrackRegion; newAction.dragObject = dragObject; newAction.snapID = trackRegionID; return newAction; }
public override void Connect (Moveable_Drag draggable) { LimitCollisions (draggable); if (doLoop) { maxAngle = 360f; } }
/** * <summary>Positions an object on a specific point along the track.</summary> * <param name = "proportionAlong">The proportion along which to place the Moveable_Drag object (0 to 1)</param> * <param name = "draggable">The Moveable_Drag object to reposition</param> */ public override void SetPositionAlong(float proportionAlong, Moveable_Drag draggable) { draggable.transform.position = transform.position + (transform.up * proportionAlong * maxDistance); if (rotationType != DragRotationType.None) { SetRotation(draggable, proportionAlong); } }
public override void SetPositionAlong (float proportionAlong, Moveable_Drag draggable) { draggable.transform.position = transform.position + (transform.up * proportionAlong * maxDistance); if (rotationType != DragRotationType.None) { SetRotation (draggable, proportionAlong); } }
/** * <summary>Applies a force to an object connected to the track.</summary> * <param name = "force">The drag force vector input by the player</param> * <param name = "draggable">The Moveable_Drag object to apply the force to</param> */ public override void ApplyDragForce(Vector3 force, Moveable_Drag draggable) { float dotProduct = Vector3.Dot(force, draggable.transform.up); // Calculate the amount of force along the tangent Vector3 tangentForce = draggable.transform.up * dotProduct; draggable._rigidbody.AddForce(tangentForce); }
public override void Connect(Moveable_Drag draggable) { if (maxRevolutions < 1) { maxRevolutions = 1; } LimitCollisions(draggable); }
public override void Connect(Moveable_Drag draggable) { LimitCollisions(draggable); if (doLoop) { maxAngle = 360f; } }
/** * <summary>Creates a new instance of the 'Object: Check track position' Action</summary> * <param name = "dragObject">The moveable object to query</param> * <param name = "trackPosition">The track position to check for</param> * <param name = "condition">The condition to make</param> * <param name = "errorMargin">The maximum difference between the queried track position, and the true track position, for the condition to be met</param> * <returns>The generated Action</returns> */ public static ActionTrackCheck CreateNew (Moveable_Drag dragObject, float trackPosition, IntCondition condition = IntCondition.MoreThan, float errorMargin = 0.05f) { ActionTrackCheck newAction = (ActionTrackCheck) CreateInstance <ActionTrackCheck>(); newAction.method = TrackCheckMethod.PositionValue; newAction.dragObject = dragObject; newAction.checkPosition = trackPosition; newAction.errorMargin = errorMargin; return newAction; }
/** * <summary>Updates the position of an object connected to the track. This is called every frame.</summary> * <param name = "draggable">The Moveable_Drag object to update the position of</param> */ public virtual void UpdateDraggable(Moveable_Drag draggable) { draggable.trackValue = GetDecimalAlong(draggable); if (!onlySnapOnPlayerRelease) { DoSnapCheck(draggable); } }
/** * <summary>Updates the position of an object connected to the track. This is called every frame.</summary> * <param name = "draggable">The Moveable_Drag object to update the position of</param> */ public override void UpdateDraggable(Moveable_Drag draggable) { SnapToTrack(draggable, false); draggable.trackValue = GetDecimalAlong(draggable); if (rotationType != DragRotationType.None) { SetRotation(draggable, draggable.trackValue); } }
public override void AssignValues(List <ActionParameter> parameters) { runtimeDragObject = AssignFile <Moveable_Drag> (parameters, dragParameterID, dragConstantID, dragObject); checkPosition = AssignFloat(parameters, checkPositionParameterID, checkPosition); checkPosition = Mathf.Max(0f, checkPosition); checkPosition = Mathf.Min(1f, checkPosition); snapID = AssignInteger(parameters, snapParameterID, snapID); }
public override void SnapToTrack(Moveable_Drag draggable, bool onStart) { draggable.transform.position = transform.position; if (onStart) { draggable.transform.rotation = transform.rotation; draggable.trackValue = 0f; } }
/** * <summary>Updates the position of an object connected to the track. This is called every frame.</summary> * <param name = "draggable">The Moveable_Drag object to update the position of</param> */ public override void UpdateDraggable(Moveable_Drag draggable) { draggable.trackValue = GetDecimalAlong(draggable); SnapToTrack(draggable, false); if (!doLoop) { UpdateColliders(draggable.trackValue, draggable); } }
public override void ApplyDragForce(Vector3 force, Moveable_Drag draggable) { float dotProduct = GetForceDotProduct(force, draggable); switch (draggable.dragTrackDirection) { case DragTrackDirection.ForwardOnly: if (dotProduct < 0f) { return; } break; case DragTrackDirection.BackwardOnly: if (dotProduct > 0f) { return; } break; default: break; } // Calculate the amount of force along the tangent Vector3 tangentForce = Transform.up * dotProduct; if (rotationType == DragRotationType.Screw) { if (dragMustScrew) { // Take radius into account tangentForce = (Transform.up * dotProduct).normalized * force.magnitude; tangentForce /= Mathf.Sqrt((draggable.GetGrabPosition() - draggable.Transform.position).magnitude) / 0.4f; } tangentForce /= Mathf.Sqrt(screwThread); } if (draggable.UsesRigidbody) { draggable.Rigidbody.AddForce(tangentForce, ForceMode.Force); } else { float normalizedDotProduct = GetForceDotProduct(force.normalized, draggable); if (Mathf.Abs(normalizedDotProduct) < 0.3f) { return; } float newPosition = draggable.trackValue + (dotProduct); ApplyAutoForce(newPosition, 0.01f * Time.deltaTime / draggable.simulatedMass, draggable, false); } }
/** * <summary>Called when an object attached to the track is disconnected from it.</summary> * <param name = "draggable">The Moveable_Drag object being disconnected from the track</param> */ public void OnDisconnect(Moveable_Drag draggable) { if (draggable.maxCollider) { Destroy(draggable.maxCollider.gameObject); } if (draggable.minCollider) { Destroy(draggable.minCollider.gameObject); } }
/** * <summary>Applies a force that, when applied every frame, pushes an object connected to the track towards a specific point along it.</summary> * <param name = "_position">The proportiona along which to place the Moveable_Drag object (0 to 1)</param> */ public override void ApplyAutoForce(float _position, float _speed, Moveable_Drag draggable) { Vector3 tangentForce = draggable.transform.forward * _speed; if (draggable.trackValue < _position) { draggable._rigidbody.AddTorque (tangentForce * Time.deltaTime); } else { draggable._rigidbody.AddTorque (-tangentForce * Time.deltaTime); } }
public override void Connect (Moveable_Drag draggable) { if (draggable._rigidbody.useGravity) { draggable._rigidbody.useGravity = false; Debug.LogWarning ("Curved tracks do not work with Rigidbodys that obey gravity - disabling"); } startPosition = transform.position + (radius * transform.right); if (doLoop) { maxAngle = 360f; base.AssignColliders (draggable); return; } AssignColliders (draggable); }
public virtual void AssignColliders (Moveable_Drag draggable) { draggable.maxCollider.transform.rotation = Quaternion.AngleAxis (90f, draggable.maxCollider.transform.right) * draggable.maxCollider.transform.rotation; draggable.minCollider.transform.rotation = Quaternion.AngleAxis (90f, draggable.minCollider.transform.right) * draggable.minCollider.transform.rotation; if (colliderMaterial) { draggable.maxCollider.material = colliderMaterial; draggable.minCollider.material = colliderMaterial; } draggable.maxCollider.transform.parent = this.transform; draggable.minCollider.transform.parent = this.transform; draggable.maxCollider.name = draggable.name + "_UpperLimit"; draggable.minCollider.name = draggable.name + "_LowerLimit"; LimitCollisions (draggable); }
public override void AssignColliders (Moveable_Drag draggable) { if (draggable.maxCollider == null) { draggable.maxCollider = (Collider) Instantiate (Resources.Load ("DragCollider", typeof (Collider))); } if (draggable.minCollider == null) { draggable.minCollider = (Collider) Instantiate (Resources.Load ("DragCollider", typeof (Collider))); } draggable.maxCollider.transform.position = transform.position + (transform.up * maxDistance) + (transform.up * draggable.colliderRadius); draggable.minCollider.transform.position = transform.position - (transform.up * draggable.colliderRadius); draggable.minCollider.transform.up = transform.up; draggable.maxCollider.transform.up = -transform.up; base.AssignColliders (draggable); }
/** * <summary>Applies a force to an object connected to the track.</summary> * <param name = "force">The drag force vector input by the player</param> * <param name = "draggable">The Moveable_Drag object to apply the force to</param> */ public override void ApplyDragForce(Vector3 force, Moveable_Drag draggable) { float dotProduct = 0f; Vector3 axisOffset = Vector2.zero; if (!alignDragToFront) { dotProduct = Vector3.Dot (force, draggable.transform.up); // Invert force if on the "back" side axisOffset = GetAxisOffset (draggable.GetGrabPosition ()); if (Vector3.Dot (draggable.transform.right, axisOffset) < 0f) { dotProduct *= -1f; } } else { // Use the Hinge's transform, not the Draggable's dotProduct = Vector3.Dot (force, transform.up); // Invert force if on the "back" side axisOffset = GetAxisOffset (draggable._dragVector); if (Vector3.Dot (transform.right, axisOffset) < 0f) { dotProduct *= -1f; } } // Calculate the amount of force along the tangent Vector3 tangentForce = (draggable.transform.forward * dotProduct).normalized; tangentForce *= force.magnitude; // Take radius into account tangentForce /= axisOffset.magnitude / 0.43f; draggable._rigidbody.AddTorque (tangentForce); }
override public void ShowGUI (List<ActionParameter> parameters) { dragObject = (Moveable_Drag) EditorGUILayout.ObjectField ("Drag object:", dragObject, typeof (Moveable_Drag), true); if (dragObject != null && dragObject.dragMode != DragMode.LockToTrack) { EditorGUILayout.HelpBox ("The chosen Drag object must be in 'Lock To Track' mode", MessageType.Warning); } EditorGUILayout.BeginHorizontal (); condition = (IntCondition) EditorGUILayout.EnumPopup (condition); checkPosition = EditorGUILayout.Slider (checkPosition, 0f, 1f); EditorGUILayout.EndHorizontal (); if (condition == IntCondition.EqualTo || condition == IntCondition.NotEqualTo) { errorMargin = EditorGUILayout.Slider ("Error margin:", errorMargin, 0f, 1f); } }
/** * <summary>Gets the proportion along the track that an object is positioned.</summary> * <param name = "draggable">The Moveable_Drag object to check the position of</param> * <returns>The proportion along the track that the Moveable_Drag object is positioned (0 to 1)</returns> */ public override float GetDecimalAlong(Moveable_Drag draggable) { float angle = Vector3.Angle (-transform.right, draggable.transform.position - transform.position); // Sign of angle? if (angle < 170f && Vector3.Dot (draggable.transform.position - transform.position, transform.up) < 0f) { angle *= -1f; } return ((180f - angle) / maxAngle); }
/** * <summary>Positions an object on a specific point along the track.</summary> * <param name = "proportionAlong">The proportion along which to place the Moveable_Drag object (0 to 1)</param> * <param name = "draggable">The Moveable_Drag object to reposition</param> */ public override void SetPositionAlong(float proportionAlong, Moveable_Drag draggable) { Quaternion rotation = Quaternion.AngleAxis (proportionAlong * maxAngle, transform.forward); draggable.transform.position = RotatePointAroundPivot (startPosition, transform.position, rotation); draggable.transform.rotation = Quaternion.AngleAxis (proportionAlong * maxAngle, transform.forward) * transform.rotation; if (!doLoop) { UpdateColliders (proportionAlong, draggable); } }
/** * <summary>Corrects the position of an object so that it is placed along the track.</summary> * <param name = "draggable">The Moveable_Drag object to snap onto the track</param> * <param name = "onStart">Is True if the game has just begun (i.e. this function is being run for the first time)</param> */ public override void SnapToTrack(Moveable_Drag draggable, bool onStart) { Vector3 LookAt = draggable.transform.position - transform.position; draggable.transform.position = transform.position + LookAt / (LookAt.magnitude / radius); if (onStart) { float proportionAlong = GetDecimalAlong (draggable); if (proportionAlong < 0f) { proportionAlong = 0f; } else if (proportionAlong > 1f) { proportionAlong = 1f; } draggable.transform.rotation = Quaternion.AngleAxis (proportionAlong * maxAngle, transform.forward) * transform.rotation; SetPositionAlong (proportionAlong, draggable); } else { draggable.transform.rotation = Quaternion.AngleAxis (draggable.trackValue * maxAngle, transform.forward) * transform.rotation; } }
public override void Connect (Moveable_Drag draggable) { AssignColliders (draggable); }
/** * <summary>Connects an object to the track when the game begins.</summary> * <param name = "draggable">The Moveable_Drag object to connect to the track</param> */ public virtual void Connect(Moveable_Drag draggable) { }
/** * <summary>Corrects the position of an object so that it is placed along the track.</summary> * <param name = "draggable">The Moveable_Drag object to snap onto the track</param> * <param name = "onStart">Is True if the game has just begun (i.e. this function is being run for the first time)</param> */ public override void SnapToTrack(Moveable_Drag draggable, bool onStart) { draggable.transform.position = transform.position; if (onStart) { draggable.transform.rotation = transform.rotation; draggable.trackValue = 0f; } }
/** * <summary>Positions an object on a specific point along the track.</summary> * <param name = "proportionAlong">The proportion along which to place the Moveable_Drag object (0 to 1)</param> * <param name = "draggable">The Moveable_Drag object to reposition</param> */ public override void SetPositionAlong(float proportionAlong, Moveable_Drag draggable) { draggable.transform.position = transform.position; draggable.transform.rotation = Quaternion.AngleAxis (proportionAlong * maxAngle, transform.forward) * transform.rotation; }
/** * <summary>Gets the proportion along the track that an object is positioned.</summary> * <param name = "draggable">The Moveable_Drag object to check the position of</param> * <returns>The proportion along the track that the Moveable_Drag object is positioned (0 to 1)</returns> */ public override float GetDecimalAlong(Moveable_Drag draggable) { float angle = Vector3.Angle (transform.up, draggable.transform.up); if (Vector3.Dot (-transform.right, draggable.transform.up) < 0f) { angle = 360f - angle; } if (angle > 180f + maxAngle / 2f) { angle = 0f; } return (angle / maxAngle); }
/** * Overrides the base (DragTrack) AssignColliders() function and does nothing. */ public override void AssignColliders(Moveable_Drag draggable) { return; }
private void UpdateColliders(float trackValue, Moveable_Drag draggable) { if (trackValue > 1f) { return; } if (trackValue > 0.5f) { draggable.minCollider.isTrigger = true; draggable.maxCollider.isTrigger = false; } else { draggable.minCollider.isTrigger = false; draggable.maxCollider.isTrigger = true; } }
private void SetRotation (Moveable_Drag draggable, float proportionAlong) { float angle = proportionAlong * maxDistance / draggable.colliderRadius / 2f * Mathf.Rad2Deg; if (rotationType == DragRotationType.Roll) { draggable._rigidbody.rotation = Quaternion.AngleAxis (angle, transform.forward) * transform.rotation; } else if (rotationType == DragRotationType.Screw) { draggable._rigidbody.rotation = Quaternion.AngleAxis (angle * screwThread, transform.up) * transform.rotation; } }
/** * <summary>Gets the proportion along the track that an object is positioned.</summary> * <param name = "draggable">The Moveable_Drag object to check the position of</param> * <returns>The proportion along the track that the Moveable_Drag object is positioned (0 to 1)</returns> */ public virtual float GetDecimalAlong(Moveable_Drag draggable) { return 0f; }
/** * <summary>Updates the position of an object connected to the track. This is called every frame.</summary> * <param name = "draggable">The Moveable_Drag object to update the position of</param> */ public override void UpdateDraggable(Moveable_Drag draggable) { float oldValue = draggable.trackValue; draggable.transform.position = transform.position; draggable.trackValue = GetDecimalAlong (draggable); if (draggable.trackValue <= 0f || draggable.trackValue > 1f) { if (draggable.trackValue < 0f) { draggable.trackValue = 0f; } else if (draggable.trackValue > 1f) { draggable.trackValue = 1f; } SetPositionAlong (draggable.trackValue, draggable); draggable._rigidbody.angularVelocity = Vector3.zero; } if (doLoop && limitRevolutions) { if (oldValue < 0.1f && draggable.trackValue > 0.9f) { draggable.revolutions --; } else if (oldValue > 0.9f && draggable.trackValue < 0.1f) { draggable.revolutions ++; } if (draggable.revolutions < 0) { draggable.revolutions = 0; draggable.trackValue = 0f; SetPositionAlong (draggable.trackValue, draggable); draggable._rigidbody.angularVelocity = Vector3.zero; } else if (draggable.revolutions > maxRevolutions - 1) { draggable.revolutions = maxRevolutions - 1; draggable.trackValue = 1f; SetPositionAlong (draggable.trackValue, draggable); draggable._rigidbody.angularVelocity = Vector3.zero; } } }
public override void ShowGUI(List<ActionParameter> parameters) { dragParameterID = Action.ChooseParameterGUI ("Moveable object:", parameters, dragParameterID, ParameterType.GameObject); if (dragParameterID >= 0) { dragConstantID = 0; dragObject = null; } else { dragObject = (Moveable_Drag) EditorGUILayout.ObjectField ("Moveable object:", dragObject, typeof (Moveable_Drag), true); dragConstantID = FieldToID <Moveable_Drag> (dragObject, dragConstantID); dragObject = IDToField <Moveable_Drag> (dragObject, dragConstantID, false); if (dragObject != null && dragObject.dragMode != DragMode.LockToTrack) { EditorGUILayout.HelpBox ("The chosen Drag object must be in 'Lock To Track' mode", MessageType.Warning); } } positionParameterID = Action.ChooseParameterGUI ("New track position:", parameters, positionParameterID, ParameterType.Float); if (positionParameterID < 0) { positionAlong = EditorGUILayout.Slider ("New track position:", positionAlong, 0f, 1f); } isInstant = EditorGUILayout.Toggle ("Is instant?", isInstant); if (!isInstant) { speed = EditorGUILayout.FloatField ("Movement speed:", speed); removePlayerControl = EditorGUILayout.Toggle ("Remove player control?", removePlayerControl); stopOnCollide = EditorGUILayout.Toggle ("Stop if has collision?", stopOnCollide); if (stopOnCollide) { layerMask = AdvGame.LayerMaskField ("'Stop' collision layer(s):", layerMask); } willWait = EditorGUILayout.Toggle ("Wait until finish?", willWait); } AfterRunningOption (); }
/** * <summary>Updates the position of an object connected to the track. This is called every frame.</summary> * <param name = "draggable">The Moveable_Drag object to update the position of</param> */ public override void UpdateDraggable(Moveable_Drag draggable) { draggable.trackValue = GetDecimalAlong (draggable); SnapToTrack (draggable, false); if (!doLoop) { UpdateColliders (draggable.trackValue, draggable); } }
public override void UpdateDraggable (Moveable_Drag draggable) { SnapToTrack (draggable, false); draggable.trackValue = GetDecimalAlong (draggable); if (rotationType != DragRotationType.None) { SetRotation (draggable, draggable.trackValue); } }
public override float GetDecimalAlong (Moveable_Drag draggable) { return (draggable.transform.position - transform.position).magnitude / maxDistance; }