// Update is called once per frame private void Update() { if (isGrounded) { var facingMultiplier = IsFacingRight ? (sbyte)1 : (sbyte)-1; switch (GroundMovementState) { case GroundMovementState.Stop: moveDirection = Vector3.zero; break; case GroundMovementState.MoveForward: //TryOrientateCharacter(); moveDirection.x = facingMultiplier * moveSpeed; break; case GroundMovementState.Jump: //TryOrientateCharacter(); moveDirection.y = JumpSpeed; if (canJumpForward) { moveDirection.x = facingMultiplier * moveSpeed; } if (jumpDelay > 0) { StartCoroutine(JumpWaiter(jumpDelay)); } break; case GroundMovementState.Patrol: if (!currentWaypoint) { currentWaypoint = waypoints[waypointIndex]; } var difference = currentWaypoint.position - transform.position; float distanceX = Mathf.Abs(difference.x); if (distanceX > .1f && difference.x != 0) { moveDirection.x = Mathf.Sign(difference.x) * moveSpeed; transform.eulerAngles = difference.x > 0f ? Vector3.zero : 180 * Vector3.up; } else { StartCoroutine(ArriveAtWaypoint(arriveDelay)); } break; case GroundMovementState.Dash: break; case GroundMovementState.Chase: //var tempFacingMultiplier = player.transform.position.x > transform.position.x ? (sbyte)1 : (sbyte)-1; //IsFacingRight = facingMultiplier == tempFacingMultiplier ? IsFacingRight : !IsFacingRight; //facingMultiplier = IsFacingRight ? (sbyte)1 : (sbyte)-1; //OrientateCharacter(); //moveDirection.x = facingMultiplier * moveSpeed; //if (!hasChased) //{ // facingMultiplier = player.transform.position.x > transform.position.x ? (sbyte)1 : (sbyte)-1; // moveDirection.x = facingMultiplier * moveSpeed; // hasChased = true; //} //transform.position = Vector2.MoveTowards(transform.position, new Vector2(player.transform.position.x, transform.position.y), moveSpeed * Time.deltaTime / 2); var playerDifference = player.transform.position - transform.position; if (!hasChased) { transform.position = Vector2.MoveTowards(transform.position, new Vector2(player.transform.position.x, transform.position.y), moveSpeed * Time.deltaTime); transform.eulerAngles = playerDifference.x > 0f ? Vector3.zero : 180 * Vector3.up; hasChased = true; } break; case GroundMovementState.Attack: //timer = defaultTimer; print("\t\t\tATTACKING"); moveDirection = Vector3.zero; break; default: break; } } moveDirection.y -= gravity * Time.deltaTime; CharacterController.move(moveDirection * Time.deltaTime); Flags = CharacterController.collisionState; isGrounded = Flags.below; if (GroundMovementState.Equals(GroundMovementState.Chase)) { OrientateCharacter(); } else { TryOrientateCharacter(); } hit = Physics2D.Raycast(raycast.position, (IsFacingRight ? (sbyte)-1 : (sbyte)1) * Vector2.left, raycastLength, layerMask); DebugRaycast(); if (hit.collider && hit.collider.tag == "Player") { distance = Vector2.Distance(transform.position, player.transform.position); if (distance > attackDistance || hasCooldown) { GroundMovementState = GroundMovementState.Chase; } else { GroundMovementState = GroundMovementState.Attack; } } else { //GroundMovementState = defaultGroundMovementState; } }
void Update() { if (isGrounded) { //STOP if (groundMovementState.Equals(GroundMovementState.Stop)) { _moveDirection = Vector3.zero; if (isGrounded && !jumpAndWait) { StartCoroutine("MoveForwardFromStop"); } //rb.velocity = new Vector2(0, 0); //rb.angularVelocity = 0f; } //MOVE FORWARD else if (groundMovementState.Equals(GroundMovementState.MoveForward)) { if (_isFacingLeft) { _moveDirection.x = -moveSpeed; } else { _moveDirection.x = moveSpeed; } } //JUMP else if (groundMovementState.Equals(GroundMovementState.Jump)) { jumpAndWait = true; if (jumpAndWait) { StartCoroutine(JumpAndWait()); } _moveDirection.y = jumpSpeed; if (jumpForward && _isFacingLeft) { _moveDirection.x = -moveSpeed; } else if (jumpForward && !_isFacingLeft) { _moveDirection.x = moveSpeed; } } ////PATROL //else if (groundMovementState.Equals(GroundMovementState.Patrol)) //{ // if (!_currentTarget) // _currentTarget = wayPoints[_wayPointCounter]; // Vector3 difference = _currentTarget.position - transform.position; // float distanceX = Mathf.Abs(difference.x); // if(distanceX > 0.1f) // { // //current target is to the right of the enemy // if(difference.x > 0f) // { // _moveDirection.x = moveSpeed; // transform.eulerAngles = new Vector3(0, 180, 0); // } // else if (difference.x < 0f) // { // _moveDirection.x = -moveSpeed; // transform.eulerAngles = new Vector3(0, 0, 0); // } // } // else // { // StartCoroutine("ArriveAtWaypoint"); // } //} //else if (groundMovementState.Equals(GroundMovementState.Dash)) //{ //} } // if isGrounded Ends HERE _moveDirection.y -= gravity * Time.deltaTime; _characterController.move(_moveDirection * Time.deltaTime); //flags, will tell you if your controler has collided (asdw) _flags = _characterController.collisionState; //detect if the E is on the ground, if it is then isGrounded = true isGrounded = _flags.below; if (autoTurn) { if (_flags.left && _isFacingLeft) { Turn(); } else if (_flags.right && !_isFacingLeft) { Turn(); } } }