private void Movement() { List <float> distanceToGround = new List <float>(); foreach (Transform transform in (verticalVelocity > 0)?transforms[SIDES.TOP]:transforms[SIDES.BOTTOM]) { distanceToGround.Add(CollisionDistance(transform, (verticalVelocity > 0) ? Vector2.up : Vector2.down, Mathf.Abs(verticalVelocity * Time.deltaTime))); } if ((ContactingSide(SIDES.BOTTOM) && verticalVelocity > 0) || distanceToGround.TrueForAll(x => x == 0)) { gameObject.transform.Translate(Vector2.up * verticalVelocity * Time.deltaTime); } else { // our current velocity will move us underground, move the object directly to ground level gameObject.transform.Translate(Vector2.up * Mathf.Sign(verticalVelocity) * (Min(distanceToGround) - objectSpacing)); verticalVelocity = 0; } List <float> distanceToWall = new List <float>(); // Find the side that the object is moving towards SIDES moveTowards = (Mathf.Sign(horizontalVelocity) > 0) ? SIDES.RIGHT : SIDES.LEFT; foreach (Transform transform in transforms[moveTowards]) { distanceToWall.Add( CollisionDistance(transform, Vector2.right * Mathf.Sign(horizontalVelocity), Mathf.Abs(horizontalVelocity * Time.deltaTime)) ); } if (distanceToWall.TrueForAll(x => x == 0)) { gameObject.transform.Translate(Vector2.right * horizontalVelocity * Time.deltaTime); } else { gameObject.transform.Translate(Vector2.right * Mathf.Sign(horizontalVelocity) * (Max(distanceToWall) - objectSpacing)); horizontalVelocity = 0; //verticalVelocity = 0; } }
private HashSet <GameObject> CheckGroundColliders(SIDES side, Vector2 dir) { HashSet <GameObject> toucing = new HashSet <GameObject>(); List <GameObject> colliders = new List <GameObject>(); foreach (Transform x in transforms[side]) { GameObject temp = RaycastCollision(x.position, dir, objectSpacing * 2); if (temp != null) { toucing.Add(temp); if (temp.GetComponent(typeof(Assets.Scripts.Tiles.Ground.IGround)) != null) { colliders.Add(temp); } } } sides[side] = colliders; return(toucing); }
public virtual void Start(CharacterScript character) { this.GroundMask = LayerMask.GetMask("Ground"); this.character = character; colliders = gameObject.transform.Find("ColliderPoints"); foreach (Transform t in colliders) { System.Text.RegularExpressions.MatchCollection matchCollection = System.Text.RegularExpressions.Regex.Matches(t.name, "[a-z]+|[A-Z]([a-z])*"); foreach (System.Text.RegularExpressions.Match match in matchCollection) { try { SIDES side = (SIDES)System.Enum.Parse(typeof(SIDES), match.Value.ToUpper()); transforms[side].Add(t); } catch (System.ArgumentException ex) { Debug.Log("Collider point " + ex + " is not a valid side"); } } } }
public bool ContactingSide(SIDES side) { return(this.sides[side].Count > 0); }
public bool FlushWithSide(SIDES side) { return(transforms[side].Count == this.sides[side].Count); }