private void Update() { if (!isLocked) { if (Time.time > randomMoveTimer + randomMoveInterval) { randomMoveTimer = Time.time; Vector2 tarPoint = PlayerCharacter.instance.GetPosition() + Mathf2.SelectRandomPoint(randomPointRange); Vector2 dir = tarPoint - uComponent.GetPosition(); uComponent.MoveTo(dir); } } else { if (Time.time > folllowUpdateInterval + followUpdateTimer) { followUpdateTimer = Time.time; randomMoveTimer = Time.time; Vector2 tarPoint = PlayerCharacter.instance.GetPosition(); Vector2 dir = tarPoint - uComponent.GetPosition(); uComponent.MoveTo(dir); } } }
/// <summary> /// The <see cref="SteeringBehaviour.ResultDirection"/> is the direction to the desired <see /// cref="AIMArrive.Target"/>. The <see cref="SteeringBehaviour.ResultMagnitude"/> is obtained by mapping the /// current position of the agent to the two given radii. /// <para/> /// Note, with an inverse <see cref="RadiusSteeringBehaviour.RadiusMapping"/> the following holds true: the /// closer the agent is to the target, the greater the velocity increases. /// </summary> /// <returns>Returns always <c>true</c>.</returns> protected override bool StartSteering() { ResultDirection = percept.Position - self.Position; sqrOuterRadius = OuterRadius * OuterRadius; sqrInnerRadius = InnerRadius * InnerRadius; // If true, then agent is within the radius if (ResultDirection.sqrMagnitude < sqrOuterRadius) { // If true, then decreasing if (RadiusMapping == MappingType.Linear || RadiusMapping == MappingType.Quadratic || RadiusMapping == MappingType.SquareRoot) { ResultMagnitude = Mathf2.MapLinear(0f, BaseMagnitude, 0f, 1f, MapSpecialSqr(RadiusMapping, sqrInnerRadius, sqrOuterRadius, ResultDirection.sqrMagnitude)); } else { ResultMagnitude = Mathf2.MapLinear(BaseMagnitude, 1f, 0f, 1f, MapSpecialSqr(RadiusMapping, sqrInnerRadius, sqrOuterRadius, ResultDirection.sqrMagnitude)); } } else { ResultMagnitude = BaseMagnitude; } return(true); }
//-------------------------------------------------------------------------------------------------------------- private void FixedUpdate() { // Find out the relation of the current movement direction to the decided direction up = transform.up; angleDiff = Vector3.Angle(up, Context.DecidedDirection); cross = Vector3.Cross(up, Context.DecidedDirection); // Do not let the cross.z be close to zero, otherwise, the character stuck when the decision direction is // anti-parallel if (!Mathf2.Approximately(Context.DecidedDirection.sqrMagnitude, 0f) && Mathf2.Approximately(cross.z, 0f)) { cross.z = Mathf.Sign(Random.Range(-1f, 1f)); } // Orient towards decision direction using torque Body2D.AddTorque(cross.z * Torque * angleDiff); // Translate along oriented direction using the force (which may be with you, of course) if (ObjectiveAsSpeed >= 0 && ObjectiveAsSpeed < Context.DecidedValues.Count) { velocity = Context.DecidedValues[ObjectiveAsSpeed] * Speed; velocity = velocity > Speed ? Speed : velocity; } else { velocity = Speed; } Body2D.AddForce(velocity * up); }
public void Drag(BaseEventData bed) { PointerEventData ped = (PointerEventData)bed; if (ped.position.x < scWidth / 2) { Vector2 touchPos = ped.position; Vector2 dir = (touchPos - leftDefPos).normalized; float angle = Vector2.Angle(dir, Vector2.right); angle = Mathf.Round(angle / 45) * 45; Vector2 newDir = Mathf2.DegreeToVector2(angle); newDir.y *= Mathf.Sign(dir.y); if (newDir.magnitude == 0) { return; } moveDir = newDir; if ((touchPos - leftDefPos).magnitude > scWidth / 15) { leftDefPos = touchPos - dir * scWidth / 20; } } }
private void RotateHead(Quaternion q) { q *= Quaternion.Euler(0, 180, 0); Vector3 e = q.eulerAngles; tHead.rotation = Quaternion.Euler(0, 0, Mathf2.ClampAngle(-e.z, -10f, 10f)); }
public static void ReadBlendShapes(string s, out List <Dictionary <string, float> > list, out List <float> times, out List <Vector3> pos, out List <Quaternion> rot, out List <Quaternion> camrot) { list = new List <Dictionary <string, float> > (); times = new List <float> (); pos = new List <Vector3>(); rot = new List <Quaternion>(); camrot = new List <Quaternion>(); if (s.Length > 3) { string[] str = Parse.CaretSV(s); Debug.Log("TotalBlendShapeFrames: " + str.Length); for (int i = 0; i < str.Length; i++) { string[] sstar = Parse.StarSV(str[i]); string processed = sstar [0]; string[] csv = Parse.CSV(processed); float t = Mathf2.String2Float(csv [0]); processed = DumpDelimiterAfter(1, ",", csv); list.Add(ParseBlendlet(processed)); times.Add(t); if (sstar.Length > 1) { string[] ttemp = Parse.TSV(sstar[1]); pos.Add(Mathf2.String2Vector3(ttemp[0])); rot.Add(Mathf2.String2Quat(ttemp[1])); if (ttemp.Length > 2) { camrot.Add(Mathf2.String2Quat(ttemp[2])); } } /* if (i == 0) * Debug.Log(DumpAppleKeys (processed)); * if (i == 0) * Debug.Log(DumpAppleKeysEnum(processed));*/ } if (times [0] > 1f) { int timedelta = Mathf.FloorToInt(times[0]); for (int i = 0; i < times.Count; i++) { times [i] -= timedelta; } } } else { Debug.Log(s.Length); } }
/// <summary> /// Maps a <paramref name="value"/> lying between <paramref name="min"/> and <paramref name="max"/> to a /// resulting value between 0 and 1. /// <para/> /// The function used for the mapping is specified via the <paramref name="mapping"/> parameter, see <see /// cref="MappingType"/>. /// </summary> /// <param name="mapping">Specifies the applied type of the mapping function.</param> /// <param name="min">The minimum of the function argument interval.</param> /// <param name="max">The maximum of the function argument interval.</param> /// <param name="value">The argument value to be mapped.</param> /// <returns>The mapped function value.</returns> public static float MapSpecial(MappingType mapping, float min, float max, float value) { if (mapping == MappingType.Constant) { return(1f); } // Clamp if value is outside of the min/max interval if (value < min + Mathf2.Epsilon) { return((int)mapping % 2 != 0 ? 0f : 1f); } else if (value > max - Mathf2.Epsilon) { return((int)mapping % 2 != 0 ? 1f : 0f); } // Map according to the specified mapping type float result; switch (mapping) { case MappingType.Linear: result = Mathf2.MapLinear(0f, 1f, min, max, value); break; case MappingType.InverseLinear: result = 1f - Mathf2.MapLinear(0f, 1f, min, max, value); break; case MappingType.Quadratic: result = Mathf2.MapLinear(0f, 1f, min, max, value); result *= result; break; case MappingType.InverseQuadratic: result = Mathf2.MapLinear(0f, 1f, min, max, value); result *= result; result = 1f - result; break; case MappingType.SquareRoot: result = Mathf.Sqrt(Mathf2.MapLinear(0f, 1f, min, max, value)); break; case MappingType.InverseSquareRoot: result = 1f - Mathf.Sqrt(Mathf2.MapLinear(0f, 1f, min, max, value)); break; default: // MappingType.Constant result = 1f; break; } return(result); }
public Vector2[] Navigate(Vector2 start, Vector2 end) { var startTile = new WeightedTile() { Position = start }; var closedList = new List <WeightedTile>(); var openList = new List <WeightedTile>() { startTile }; while (openList.Any()) { var quickestTile = openList.OrderBy(t => t.Weight).First(); if (quickestTile.Position == end) // We're done! Found the path. { return(FindPath(startTile, quickestTile)); } openList.Remove(quickestTile); closedList.Add(quickestTile); foreach (var neighbor in _searchableDirections.Select(d => d.ToVector2()) .Select(d => quickestTile.Position + d) .Where(d => IsTraversable(d))) { if (closedList.Any(c => c.Position == neighbor)) { continue; } var movementCost = Mathf.RoundToInt( Mathf2.DistanceTo(neighbor, startTile.Position) + quickestTile.MovementCost); var existingNeighbor = openList.FirstOrDefault(o => o.Position == neighbor); if (existingNeighbor == null) { openList.Add(new WeightedTile() { MovementCost = movementCost, Heuristic = Mathf.RoundToInt(Mathf2.DistanceTo(neighbor, end)), Parent = quickestTile, Position = neighbor }); } else if (existingNeighbor.MovementCost > movementCost) { existingNeighbor.MovementCost = movementCost; existingNeighbor.Parent = quickestTile; } } } return(new Vector2[0]); }
static Dictionary <string, float> ParseBlendlet(string s) { Dictionary <string, float> blendlet = new Dictionary <string, float> (); string[] str = Parse.SSV(s); for (int i = 0; i < str.Length; i++) { // Debug.Log (str [i]); string[] str2 = Parse.CSV(str [i]); blendlet.Add(str2[0], Mathf2.String2Float(str2[1])); } return(blendlet); }
public void Init() { instance = this; tr = transform; radius = GetComponent <CircleCollider2D>().bounds.size.x / 2; for (int i = 0; i < starCount; i++) { Vector2 randomPoint = Mathf2.SelectRandomPoint(radius); Vector3 randomPos = new Vector3(randomPoint.x + PlayerCharacter.instance.tr.position.x, randomPoint.y + PlayerCharacter.instance.tr.position.y, UnityEngine.Random.Range(10, 30)); GameObject star = GameObject.Instantiate(starObj, randomPos, Quaternion.identity); star.transform.SetParent(starRoot.transform); star.GetComponent <SpriteRenderer>().color = Random.ColorHSV(); } }
IEnumerator InstantiateStarsOverTime() { for (int i = 0; i < starRoot.transform.childCount; i++) { if (!PlayerCharacter.instance) { break; } Vector2 randomPoint = Mathf2.SelectRandomPoint(radius); Vector3 randomPos = new Vector3(randomPoint.x + PlayerCharacter.instance.tr.position.x, randomPoint.y + PlayerCharacter.instance.tr.position.y, UnityEngine.Random.Range(10, 30)); starRoot.transform.GetChild(i).transform.position = randomPos; if (i % 20 == 0) { yield return(new WaitForEndOfFrame()); } } }
//-------------------------------------------------------------------------------------------------------------- private void Update() { if (Mathf2.Approximately(Context.DecidedDirection.sqrMagnitude, 0)) { return; } // Orient towards decision direction transform.rotation = Quaternion.LookRotation(Context.DecidedDirection, Up); // Translate along oriented direction if (ObjectiveAsSpeed >= 0) { velocity = Context.DecidedValues[ObjectiveAsSpeed] * Speed; velocity = velocity > Speed ? Speed : velocity; } else { velocity = Speed; } transform.position += Time.deltaTime * velocity * Context.DecidedDirection; }