protected virtual void HandleMovement(float input) { float newXVelocity = velocity.x; facing = (LookDirections)input; if (input != 0) { newXVelocity = Accelerate(newXVelocity, input); } //apply deceleration if I'm not pressing anything or if I'm pressing the opposite from where I'm going (turning around) if ((input == 0 && Mathf.Abs(newXVelocity) > stopLeeway) || ((input > 0 && newXVelocity < 0) || (input < 0 && newXVelocity > 0))) { newXVelocity = Decelerate(newXVelocity); } else if (Mathf.Abs(newXVelocity) <= stopLeeway) { newXVelocity = 0; } if (newXVelocity != 0) { CheckDirections checkDirection = newXVelocity > 0? CheckDirections.right : CheckDirections.left; RaycastHit2D[] hits = CollisionCheck(Mathf.Abs(newXVelocity * Time.deltaTime), MoveLayers, checkDirection, HitWall); //RaycastHit2D[] hits = CollisionCheck(Mathf.Abs(newXVelocity * Time.deltaTime), MoveLayers, CheckDirections.down, HitWall); if (hits.Length > 0) { RaycastHit2D useData = new RaycastHit2D(); foreach (RaycastHit2D hit in hits) { // if (IsAWall(hit.normal)){ if (hit.fraction > 0) { useData = hit; break; } } if (useData.fraction > 0 && velocity.x != 0) { t.position = new Vector3(useData.point.x - boxCol.size.x / 2 * (velocity.x > 0? 1 : -1), t.position.y, t.position.z); newXVelocity = 0; } } } velocity = new Vector2(newXVelocity, velocity.y); }
//generic collision check for any direction. Needs BoxCollider. protected virtual RaycastHit2D[] CollisionCheck(float distance, int mask, CheckDirections dir, CollisionEvent collideMethod) { List <RaycastHit2D> hits = new List <RaycastHit2D>(); Vector2 direction = VectorFromDirection(dir); Vector2 centre = Box.center; //in this case, start and end represent the first and last points from which we'll cast rays Vector2 startDirection = VectorFromDirection(dir == CheckDirections.left? CheckDirections.down : (dir - 1)); Vector2 endDirection = VectorFromDirection(dir == CheckDirections.down? CheckDirections.left : (dir + 1)); bool vertical = (dir == CheckDirections.down) || (dir == CheckDirections.up); float sideModifier = vertical ? Box.width - GetMargin(true): Box.height - GetMargin(false); float rayDistance = vertical? (distance + Box.height / 2) : (distance + Box.width / 2); Vector2 start = centre + startDirection * sideModifier / 2; Vector2 end = centre + endDirection * sideModifier / 2; int rayNumber = vertical? vertRays : horiRays; bool hit = false; for (int i = 0; i < rayNumber; i++) { float lerpAmount = (float)i / (float)(rayNumber - 1); Vector2 origin = Vector2.Lerp(start, end, lerpAmount); RaycastHit2D hitInfo; //Vector2 target = origin + direction * rayDistance; //for use in Linecast if that's better hitInfo = Physics2D.Raycast(origin, direction, rayDistance, MoveLayers); if (hitInfo.collider != null) { hit = true; hits.Add(hitInfo); } } if (hit) { if (collideMethod != null) { collideMethod(); } } return(hits.ToArray()); }
//---------------------------------------------------\\ //----------------Getters and setters----------------\\ //---------------------------------------------------\\ protected Vector2 VectorFromDirection(CheckDirections direction) { switch (direction) { case CheckDirections.left: return(-Vector2.right); case CheckDirections.right: return(Vector2.right); case CheckDirections.up: return(Vector2.up); case CheckDirections.down: return(-Vector2.up); default: Debug.LogWarning("Not a valid check direction!"); return(Vector2.zero); } }
private void OnEnable() { _context = GetComponent <Context>(); _checkDirections = GetComponent <CheckDirections>(); _targeting = GetComponent <Targeting>(); }