using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    public GameObject leftLeg;
    public GameObject rightLeg;
    public GameObject leftArm; // Only left arm will be affected
    Rigidbody2D leftLegRB;
    Rigidbody2D rightLegRB;
    Rigidbody2D leftArmRB; // Rigidbody2D for the left arm
    public Rigidbody2D rb;

    public Animator anim;

    [SerializeField] float speed = 1.5f;
    [SerializeField] float stepWait = 0.5f;
    [SerializeField] float jumpForce = 10;
    private bool isOnGround;
    public float positionRadius;
    public LayerMask ground;
    public Transform playerPos;

    // Increased physics force for faster arm movement
    [SerializeField] float armForce = 50f;  // Increased for stronger movement
    [SerializeField] float armDamping = 0.2f;  // Optional, control arm smoothness

    // Start is called before the first frame update
    void Start()
    {
        leftLegRB = leftLeg.GetComponent<Rigidbody2D>();
        rightLegRB = rightLeg.GetComponent<Rigidbody2D>();
        leftArmRB = leftArm.GetComponent<Rigidbody2D>(); // Getting Rigidbody2D for the left arm
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetAxisRaw("Horizontal") != 0)
        {
            if (Input.GetAxisRaw("Horizontal") > 0)
            {
                anim.Play("WalkRight");
                StartCoroutine(MoveRight(stepWait));
            }
            else
            {
                anim.Play("WalkLeft");
                StartCoroutine(MoveLeft(stepWait));
            }
        }
        else
        {
            anim.Play("Idle");
        }

        isOnGround = Physics2D.OverlapCircle(playerPos.position, positionRadius, ground);
        if (isOnGround == true && Input.GetKeyDown(KeyCode.Space))
        {
            rb.AddForce(Vector2.up * jumpForce);
        }

        MoveLeftArmToCursor();
    }

    IEnumerator MoveRight(float seconds)
    {
        leftLegRB.AddForce(Vector2.right * (speed * 1000) * Time.deltaTime);
        yield return new WaitForSeconds(seconds);
        rightLegRB.AddForce(Vector2.right * (speed * 1000) * Time.deltaTime);
    }

    IEnumerator MoveLeft(float seconds)
    {
        rightLegRB.AddForce(Vector2.left * (speed * 1000) * Time.deltaTime);
        yield return new WaitForSeconds(seconds);
        leftLegRB.AddForce(Vector2.left * (speed * 1000) * Time.deltaTime);
    }

    void MoveLeftArmToCursor()
    {
        Vector2 cursorPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition); 
        cursorPosition = new Vector2(cursorPosition.x, cursorPosition.y); // Remove the z-component

        // Calculate the direction from the arm to the cursor
        Vector2 direction = cursorPosition - (Vector2)leftArm.transform.position;

        // Apply a force to the arm's Rigidbody2D towards the cursor
        Vector2 force = direction.normalized * armForce; // Apply force in the direction of the cursor
        leftArmRB.AddForce(force, ForceMode2D.Force);

        // Optional: Apply damping to smooth out the movement
        leftArmRB.velocity = Vector2.Lerp(leftArmRB.velocity, Vector2.zero, armDamping);
    }
}

Please take this script and make sure that it doesn't move anything else exept for the arm. Not the legs or anything. And should not move the player towards the cursor.