protected IInteractable[] GetInteractablesInRange() { List <IInteractable> InteractableList = new List <IInteractable>(); Vector3 StartPos = transform.position; Vector3 EndPos = StartPos + ControlRotation * Vector3.forward * InteractRange; Collider[] InteractColliders = Physics.OverlapCapsule(StartPos, EndPos, InteractRadius); Debug.DrawLine(StartPos, EndPos, Color.blue, 2f); foreach (Collider InteractCollider in InteractColliders) { if (InteractCollider.tag == "Interactable") { Component[] HitComponents = InteractCollider.GetComponents <Component>(); foreach (Component HitComponent in HitComponents) { if (HitComponent is IInteractable) { InteractableList.Add((IInteractable)HitComponent); } } } } return(InteractableList.ToArray()); }
public static void SetInteractCollider(InteractCollider ic) { interactCollider = ic; }
// Start is called before the first frame update void Start() { controller = GetComponent <CharacterController>(); entityHit = new List <GameObject>(); interactScript = GetComponentInChildren <InteractCollider>(); }
private void UpdateMovement(GameTime gameTime, KeyboardState keyboardState) { // create movement direction vector Vector2 movement = new Vector2(); this.animationState = AnimationState.IDLE; if (keyboardState.IsKeyDown(Keys.S)) { movement += Vector2.UnitY; this.animationState = AnimationState.WALKING; this.orientation = Orientation.FRONT; } if (keyboardState.IsKeyDown(Keys.W)) { movement += -Vector2.UnitY; this.animationState = AnimationState.WALKING; this.orientation = Orientation.BACK; } if (keyboardState.IsKeyDown(Keys.D)) { movement += Vector2.UnitX; this.animationState = AnimationState.WALKING; this.orientation = Orientation.RIGHT; } if (keyboardState.IsKeyDown(Keys.A)) { movement += -Vector2.UnitX; this.animationState = AnimationState.WALKING; this.orientation = Orientation.LEFT; } // Normalize vector to prevent faster vertical movement if (movement.Length() > 0) { movement.Normalize(); } Gizmos.Instance.DrawGizmo(new LineGizmo(Transform.AbsolutePosition, Transform.AbsolutePosition + 35 * movement, 1, Color.Blue)); // draw normalized movement vector movement *= speed * (float)gameTime.ElapsedGameTime.TotalSeconds; // change direction to movement vector // Movement X // add movement to current position Transform.Position += movement * Vector2.UnitX; // update collider to current position and check colliding -> if so reset position TODO: FIXME Position should not be reset this.Collider.Update(gameTime); if (movement.X != 0 && Entity.IsColliding(this)) // if position has changed -> check colliding { Transform.Position -= movement * Vector2.UnitX; } // Movement Y // add movement to current position Transform.Position += movement * Vector2.UnitY; // update collider to current position and check colliding -> if so reset position TODO: FIXME Position should not be reset this.Collider.Update(gameTime); if (movement.Y != 0 && Entity.IsColliding(this)) // if position has changed -> check colliding { Transform.Position -= movement * Vector2.UnitY; } if (this.IsDead && !Level.Hud.DialogBox.IsDialogRunning) { Scene.ReloadScene(); } InteractCollider.Update(gameTime); }