Looking for basic scripts to follow for a dynamic 2D character build

Does anyone have any open source scripts to follow where a basic 2D character has Walk, Jump & crouch (to duck)? Ive been searching Github also. but not much luck yet. im just looking for something to study to get it to work properly to work with my script. And yes im aware it has something to do with the collision sizes n stuff. ive seen that but its getting the controller (and keyboard) to make my character crouch down by pressing down on the controller or any button i set it to on pc.

Currently my character runs, jumps and attacks. Its 8 bit Nintendo style im at my wits in why i cant get my character to crouch.

I'm not sure I understand the difficulty. 8-bit Nintendo style crouching is literally just switching to a different sprite. And of course you need to adjust your collider according to what sprite image is currently shown.

But that's obvious, so what's the trouble you're running into?

1) i dont know how to create the proper input trigger for crouch
2) im not entirely sure how the collision box trigger script should look (i try to follow tutorials on youtube but either they make it too complicated to follow or its a half job video with no completion)
&
3) The animator settings. i have a free template that gives me an idea but im seeing theres different methods to doing it smh

so basically 1st. what does the crouch input is supposed to look like exactly? Currently i got my character to run, jump and attack and i can use my keyboard or gamepad. But for some damn reason i cant figure out how to connect things right so that crouch input transforms my sprites to change to crouch animation

example below. this is part of the input i did for jump and to move horizontal. i have my attack in a different CS file

        void Update()
        {
            mX = Input.GetAxisRaw("Horizontal");

            if (Input.GetButtonDown("Jump") && Is_Grounded())

            {
              Jump();
            }

That script above is in my Playermovement

here below is an example of whats in my PlayerAnim file where the Animator parameters activate

void Update()
{
//Anims
anim.SetBool("Jumping", character.IsJumping());
anim.SetBool("InAir", !character.Is_Grounded());
anim.SetBool("isGrounded", character.Is_Grounded());

}
void OnJump()
{
anim.SetTrigger("Jump");
}

All im trying to do is get a clear idea how to start up the crouch sprite activator. im lost at this point how to do that

Well you know how to use GetAxisRaw. Assuming you want to push "down" to crouch, you'd use that same function, something like this:

bool isCrouching = false;

void Update() {
    // ...other stuff you're already doing...

    bool crouch = Input.GetAxisRaw("Vertical") < -0.5f;
    if (crouch && !isCrouching) StartCrouch();
    else if (!crouch && isCrouching) EndCrouch();
}

Then you would write StartCrouch and EndCrouch methods that change your sprite.

I personally don't much care for using Unity's Animator for this sort of thing; see my article on three ways to animate in Unity and see what you think for yourself. Depending on what approach you use, the contents of the StartCrouch and EndCrouch methods will vary. But initially, these should contain just Debug.Log statements that let you see when they are being called. Then run the game, press down on your D-pad (or use the down-arrow or S key), and verify that the messages appear as you expect.

Then tackle the next step, with a more specific question: how do you make your character transition to the crouching sprite, using whichever of the three approaches to animation you have decided to embrace?

Ahh ok i see what you did there. with the Bool crouch etc etc. And yeah i used Debug.Log before seeing an old tutorial. so ill do that too then. Thanks on this. ill look through your Article.

One more thing ... is it better to keep the input expression ("Vertical") or can i change it to ("Crouch") so it stands out?

after dinner im gonna dig in all of this

oh with crouching i was doing it with the animator way that i was trying to follow in this Black and white free template 2D platformer i have. i got more comfortable doing that since it worked for my jump and run but im always open to other options to look into to try

Input.GetAxisRaw("Crouch") would not work, because there isn't a "Crouch" axis. Or I should say there probably isn't one. You could define one in the Input Manager, if you know how. But you get a Horizontal and a Vertical axis by default, so I would recommend you just stick with those.

So if you're using the animator approach, your StartCrouch and EndCrouch methods could simply set a boolean property on the animator. And then you'd have to have your animator controller set up with transitions based on that property, from your various standing/walking states to crouching, and from crouching back to standing.

Ok im back to it. I looked over your article. really useful for damn sure. if i was starting over fresh clean again id definitely follow your entirely character build for mines. i may just do that anyway in a different build to see where it takes me in progress starting from scratch again with a script

As for the crouch yeah i know how to put it in Input Manager. I had labeled Crouch in there before but theres already the default "Vertical" as you said. but since keeping that in there is better then ill run with that then this time to try for better results for what im trying to do.

As for StartCrouch and EndCrouch would those be Void methods?

would that be better to keep that in my Playermovement script with the InputAxis for the crouch?

for example i had something like these below in there following a tutorial but it didnt work entirely smh (no compiler error but it just wasnt changing the sprites when i hit down). How would you make it look to connect with that. Ill be honest i was trying to figure out these two methods below to work with my own attempt at a crouch input.

This one that i tried to follow in a tutorial method

private void Crouch(){
            if (playerControls.crouchingPressed){
                isCrouching = true;
                playerCollider.size = crouchColliderSize;
                playerCollider.offset = crouchColliderOffset;

            }
        }

        private void StandUp(){
            if (!playerControls.crouchingPressed){
                isCrouching = false;
                playerCollider.size = standColliderSize;
                playerCollider.offset = standColliderOffset;

            }
        }
    }
}

Or this one from this Free template i have. i was trying to use that for crouch input i was figuring out at the time too in a different build

private void UpdateCrouch()
        {
            if (!can_crouch)
                return;

            //Crouch
            bool was_crouch = is_crouch;
            if (move_input.y < -0.1f && is_grounded)
            {
                is_crouch = true;
                movement = Vector2.zero;
                playerCollider.size = new Vector2(standColliderSize.x, standColliderSize.y * crouch_coll_percent);
                playerCollider.offset = new Vector2(standColliderOffset.x, standColliderOffset.y * crouch_coll_percent);

                if (!was_crouch && is_crouch)
                {
                    if (onCrouch != null)
                        onCrouch.Invoke();
                }
            }
            else
            {
                is_crouch = false;
                playerCollider.size = crouchColliderSize;
                playerCollider.offset = crouchColliderOffset;
            }
        }

how would you make either of those methods connect properly to the Bool crouch you shared?

ill be trying to see what works using one of those with it in the meantime

Ok this is what i did so far and tested it to make sure theres no compile errors. the other part ill try to connect tomorrow. Gonna get some sleep

[Header("Crouch")]
bool isCrouching = false;
bool crouch = false;



        // For initialization
        void Start()
        {

        }

        void Update()
        {
            mX = Input.GetAxisRaw("Horizontal");
            if (Input.GetButtonDown("Jump") && Is_Grounded())
            {
              Jump();
            }
            {
                bool crouch = Input.GetAxisRaw("Vertical") < -0.5f;
                if (crouch && !isCrouching) StartCrouch();
                else if (!crouch && isCrouching) EndCrouch();
            }
            //anim.SetFloat("Speed", Mathf.Abs(horizontalMove));
            if (Mathf.Abs(mX) > 0.05f){
                anim.SetBool("isRunning", true);
            }else{
                anim.SetBool("isRunning", false);
            }if (mX > 0f){
                transform.localScale = new Vector3(1f, 1f, 1f);
            }else if (mX < 0f){
                transform.localScale = new Vector3(-1f, 1f, 1f);
            }
            //
            anim.SetBool("isGrounded", Is_Grounded());
            //anim.SetBool("Crouch", IsCrouched());//
        }


        private void EndCrouch()
        {
            if (!crouch && isCrouching)
            {
                isCrouching = false;
                playerCollider.size = standColliderSize;
                playerCollider.offset = standColliderOffset;

            }
        }

was that done properly?

Almost. I'm honestly surprised that compiles; your Awake method starts on line 7-8, and looks like it's intended to end at line 23, but I see no } there. So the Awake method just never ends as far as I can tell.

Also the portion from lines 17-22 looks like it needs to be in Update (which runs every frame), not Awake (which runs only once).

Your StartCrouch and EndCrouch methods look fine, except that they only effect they have is to change the player collider size and offset. They are not speaking to your Animator. You will need to use animator.SetBool to set a boolean property in the animator, as shown in the example in the docs.

Ok i did this here just now.. obviously nothing worked with it because i did it wrong for crouch. im still looking it over. No compile errors though. i tried to add in a bool there. i thought that was the answer to get things happening

private void StartCrouch()
        {
            if (crouch && !isCrouching)
            {
                anim.SetBool("Crouch", true);
                playerCollider.size = crouchColliderSize;
                playerCollider.offset = crouchColliderOffset;


            }
        }

        private void EndCrouch()
        {
            if (!crouch && isCrouching)
            {
                anim.SetBool("Crouch", false);
                playerCollider.size = standColliderSize;
                playerCollider.offset = standColliderOffset;

            }
        }

That's a necessary step, yeah

Note that you don't need the if statements in StartCrouch and EndCrouch. If you followed my suggestion above, you've already checked those conditions before StartCrouch and EndCrouch are called. So by the time these methods are executing, they aren't supposed to check anything — they are supposed to just do what they say (start or end the crouch).

So take out the if statements, and add in Debug.Log statements so you can be certain they're being called when you think they are.

Then, if your character is still not crouching, then the problem is not in your code but in your animation controller. Check it over to make sure that you have a "Crouch" property, and that you have transitions set up to go to and from the crouching sprite based on the value of that property.

Oh ok. i thought you meant it was fine to keep it in that other post i shared.... but i think i understand why you are confused. im trying not to share that big @anon_70280526 wall of code im digging through and cleaning around. its way more code than that. ill share it

ill remove the "if" statements. i thought i was being clever by using a similar script method i was following before and tried to alter it a bit for the result i was looking for

but anyway heres the entire script

namespace prototype.Platformer
{

    public class PlayerMovement : MonoBehaviour
    {
        public PlayerAnim controller;//



        public AudioClip jumpAudio;
        public AudioClip respawnAudio;
        public AudioClip ouchAudio;
        //
        public UnityAction onJump;
        public UnityAction onLand;
        public UnityAction onCrouch;
        //
        //
        [Header("Movement")]
        public float Speed = 7f;//
        public float jumpForce = 7f;//20
        public Rigidbody2D rb;
        private BoxCollider2D playerCollider;
        //
        public LayerMask ground_Layer;
        public Transform groundPoint;
        public float groundRadius;
        //
        public Animator anim;
        public Transform ceilPoint;
        private bool ceiled;
        public bool Grounded;
        private bool is_grounded;//
        //
        [Header("Vectors")]
        private Vector2 standColliderSize;//
        private Vector2 standColliderOffset;//
        private Vector2 crouchColliderSize;//
        private Vector2 crouchColliderOffset;//
        private Vector2 movement;//move
        private Vector2 move_input;
        //
        [Header("Fall Below Level")]
        public bool reset_when_fall = true;
        public float fall_pos_y = -5f;
        public float fall_damage_percent = 0.25f;
        public float ground_raycast_dist = 0.1f;
        //
        [Header("Crouch")]
        bool isCrouching = false;
        bool crouch = false;

        public float crouch_coll_percent = 0.5f;//

        private bool is_jumping = false;
        //

        float mX;
        private bool Standing;
        private bool Crouching;
        //

        void Awake()
        {
            rb = GetComponent<Rigidbody2D>();
            anim = GetComponent<Animator>();


            playerCollider = GetComponent<BoxCollider2D>();
            //
            standColliderSize = playerCollider.size;//
            standColliderOffset = playerCollider.offset;//

            crouchColliderSize = new Vector2(standColliderSize.x, standColliderSize.y * crouch_coll_percent);//
            crouchColliderOffset = new Vector2(standColliderOffset.x, standColliderOffset.y * crouch_coll_percent);//

        }

        // For initialization
        void Start()
        {

        }

        void Update()
        {
            mX = Input.GetAxisRaw("Horizontal");
            if (Input.GetButtonDown("Jump") && Is_Grounded())
            {
              Jump();
            }

            {

                bool crouch = Input.GetAxisRaw("Vertical") < -0.5f;
                if (crouch && !isCrouching) StartCrouch();
                else if (!crouch && isCrouching) EndCrouch();
            }



            if (Mathf.Abs(mX) > 0.05f){
                anim.SetBool("isRunning", true);
            }else{
                anim.SetBool("isRunning", false);
            }if (mX > 0f){
                transform.localScale = new Vector3(1f, 1f, 1f);
            }else if (mX < 0f){
                transform.localScale = new Vector3(-1f, 1f, 1f);
            }
            //
            anim.SetBool("isGrounded", Is_Grounded());

        }

        void FixedUpdate()
        {



            Vector2 movement = new Vector2(mX * Speed, rb.velocity.y);
            Grounded = Physics2D.OverlapCircle(groundPoint.position, groundRadius, ground_Layer);
            ceiled = Physics2D.OverlapCircle(ceilPoint.position, groundRadius, ground_Layer);

            rb.velocity = movement;

            StartCrouch();

        }

        void Jump()
        {

            Vector2 movement = new Vector2(rb.velocity.x, jumpForce);

            rb.velocity = movement;
        }


        public Vector2 GetMove()
        {
            return movement;
        }

        public bool IsJumping()
        {
            return is_jumping;
        }

        private void StartCrouch()
        {
            if (crouch && !isCrouching)
            {
                anim.SetBool("Crouch", true);
                playerCollider.size = crouchColliderSize;
                playerCollider.offset = crouchColliderOffset;


            }
        }

        private void EndCrouch()
        {
            if (!crouch && isCrouching)
            {
                anim.SetBool("Crouch", false);
                playerCollider.size = standColliderSize;
                playerCollider.offset = standColliderOffset;

            }
        }

        public bool Is_Grounded()
        {
            Collider2D groundCheck = Physics2D.OverlapCircle(groundPoint.position, groundRadius, ground_Layer);//feet//0.5f//Collider2D

            if (groundCheck != null)
            {
                return true;
            }

            return false;
        }
    }
}

I was not confused by the partial script. The comments in my last response still apply.

that was a mistake post. i dont know why my post came out looking like that with 17-22 looking that way. it was late and i was sleepy at that time. pardon that. but that script above you is the exact code from my file. but anyway i fixed that past post just now for viewing

the script above your post is my actual real code from my File.