// Update is called once per frame void Update() { if (isDead) { return; } Vector2 mousePositionInRealWorld = cam.ScreenToWorldPoint(Input.mousePosition); Vector2 directionFromCharacterToMouse = (mousePositionInRealWorld - (Vector2)transform.position).normalized; crosshairTransform.position = transform.position + (Vector3)(directionFromCharacterToMouse * crosshairOffset); /////////////////////////////////////// Collider2D coll = Physics2D.OverlapPoint(crosshairTransform.position, minableLayers); if (crosshairRenderer != null) { crosshairRenderer.color = (coll != null) ? minableCrosshairColor : unminableCrosshairColor; } if (coll != null && Input.GetMouseButtonDown(0)) { Tile tile = coll.GetComponent <Tile>(); if (tile != null) { tile.Hit(force); } if (directionFromCharacterToMouse.x > 0.5f) { modelGameObject.localScale = new Vector3(1f, 1f, 1f); } else if (directionFromCharacterToMouse.x < -0.5f) { modelGameObject.localScale = new Vector3(-1f, 1f, 1f); } // else by body.velocity.x instead of crosshair if (!mining) { audioSource.Play(); mining = true; animator.SetBool("mining", true); mineStartTime = Time.fixedTime; } } else if (mining && (Time.fixedTime - mineStartTime > MINE_ANIM_DURATION)) { mining = false; animator.SetBool("mining", false); } /////////////////////////////////////// if (groundStatus == GroundStatus.Grounding && (Input.GetButtonDown("Jump") || Input.GetAxisRaw("Vertical") == 1)) { groundStatus = GroundStatus.StartingJump; jumpAudioSource.PlayDelayed(0); } }
private void FixedUpdate() { if (isDead) { return; } body.velocity = new Vector2(Input.GetAxis("Horizontal") * movementSpeed, body.velocity.y); animator.SetFloat("speedX", Mathf.Abs(body.velocity.x)); if (modelGameObject != null && body.velocity.x != 0f) { modelGameObject.localScale = new Vector3((body.velocity.x > 0f) ? 1f : -1f, 1f, 1f); } if (groundStatus == GroundStatus.StartingJump) { body.AddForce(new Vector2(Input.GetAxis("Horizontal"), jumpForce), ForceMode2D.Impulse); groundStatus = GroundStatus.Jumping; animator.SetBool("jumping", true); } }
private new void LateUpdate() { base.LateUpdate(); if (isDead) { return; } switch (groundStatus) { case GroundStatus.Grounding: break; case GroundStatus.StartingJump: // Do nothing. Wait FixedUpdate to add the impulse break; case GroundStatus.Jumping: if (body.velocity.y <= 0) { groundStatus = GroundStatus.Falling; } break; case GroundStatus.Falling: if (body.velocity.y == 0) { groundStatus = GroundStatus.Grounding; animator.SetBool("jumping", false); } break; default: break; } }