#Am i doing things wrong?

1 messages · Page 1 of 1 (latest)

dreamy frigate
analog copper
#

!code

wet bronzeBOT
analog copper
#

see the large code blocks section there please

gritty igloo
#

PlayerMovement.ProcessInput has some useless brackets you could remove.

#

Animator.SetFloat and the likes also take a hash so you're not doing string comparisons every frame.

Example:

private static readonly int MovementX = Animator.StringToHash("movementX");

private void Update()
{
  anim.SetFloat(MovementX, input.x);
}
#

I would also move all self-initialization to Awake, rather than Start. Then you can use Start to talk to other objects (if necessary) and you avoid race conditions (where things are uninitialized).

#

Other than that it's clean. Behaviour is wrapped into methods with a single responsibility. Quite nice.

dreamy frigate
#

got it, really appreciate you for your time! and yeah sorry for posting Large Code Blocks like that..

pastel crater
#

Some very minor points:

input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
  • those should use moveX and moveY instead of reading the input again
  • some methods start with a lowercase letter
  • EnemyState enum declaration should be at the top of the class before methods
  • faceDirection isn't used in calculations so it should be an enum instead of int
  • I think it's just as fast to just set all animator booleans to false in ChangeState without checking first which one to change
#

In fact it could be just:

private void ChangeState(EnemyState newState)
{
    enemyState = newState;
    anim.SetBool("isIdle", enemyState == EnemyState.Idle);
    anim.SetBool("isMoving", enemyState == EnemyState.Moving);
    anim.SetBool("isAttacking", enemyState == EnemyState.Attacking);
}
analog copper
#

also x == true can be replaced by x

#

input.x != 0 || input.y != 0
this could be input != Vector2.zero - you could use a similar thing for the preceding condition if you build a Vector2 from the inputs as well

unique sun
#

something i do because i am also using logic depending on state is

  {
      switch (state)
      {
          case MovementState.idle:
              Debug.Log("Player is idle.");
              // Add idle animation or behavior
              rb.linearVelocity = Vector3.zero; //current not working right but i think its from ground check issues
              movementState = MovementState.idle;
              break;
          case MovementState.walking:
              Debug.Log("Player is walking.");
              movementState = MovementState.walking;
              break;
          case MovementState.sprinting:
              Debug.Log("Player is sprinting.");
              movementState = MovementState.sprinting;
              break;
          case MovementState.crouching:
              Debug.Log("Player is crouching.");
              movementState = MovementState.crouching;
              break;
          case MovementState.air:
              Debug.Log("Player is in air.");
              movementState = MovementState.air;
              break;
          default:
              Debug.Log("Unknown player state.");
              break;
      }
  }
dreamy frigate
dreamy frigate