hey guys! im kinda learning to code and stuff and dont know if im doing things very messy or not ideal. kindly help me make my code better 🙂
A tool for sharing your source code with the world!
1 messages · Page 1 of 1 (latest)
hey guys! im kinda learning to code and stuff and dont know if im doing things very messy or not ideal. kindly help me make my code better 🙂
A tool for sharing your source code with the world!
!code
Use links to services like:
https://paste.mod.gg/, https://hastebin.skyra.pw/, https://paste.ofcode.org/, https://paste.myst.rs/
Surround code with three backquotes. Not quotation marks.
To format as C#, add cs to the first line:
```cs
// Your code here
```
Add a comment with a line number if there is an error message.
see the large code blocks section there please
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.
got it, really appreciate you for your time! and yeah sorry for posting Large Code Blocks like that..
Some very minor points:
input.x = Input.GetAxisRaw("Horizontal");
input.y = Input.GetAxisRaw("Vertical");
moveX and moveY instead of reading the input againEnemyState enum declaration should be at the top of the class before methodsfaceDirection isn't used in calculations so it should be an enum instead of intChangeState without checking first which one to changeIn 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);
}
also x == true can be replaced by x
input.x != 0 || input.y != 0
this could beinput != Vector2.zero- you could use a similar thing for the preceding condition if you build a Vector2 from the inputs as well
you can fix it, yknow.
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;
}
}
got it! those are some really helpful points that you stated. thanks 🙂
finally fixed XD