A
A
Alexander2021-03-23 15:03:02
Unity
Alexander, 2021-03-23 15:03:02

There are two scripts that work separately, but why not together?

There is a character with two scripts, one is responsible for what the player would patrol the platform, the other for jumping (if I press the button, the character should jump). Separately, they work, but only the patrol script works together.

//скрипт №1

     public class Moving : MonoBehaviour
    {
    
        [SerializeField] private Transform playerModelTransform;
        [SerializeField] private bool isMoovingRight = true;
        [SerializeField] private bool isFacingRight = true;
        [SerializeField] private float speed;
    
        private Rigidbody2D rb;
    
        void Start()
        {
            rb = GetComponent<Rigidbody2D>();
    
        }
    
    
        void FixedUpdate()
        {
            if (isMoovingRight && isFacingRight)
            {
                rb.velocity = new Vector2(speed, 0f);
            }
            else if (!isMoovingRight && !isFacingRight)
            {
                rb.velocity = new Vector2(-speed, 0f);
            }
    
    
        }
    
        private void OnTriggerEnter2D(Collider2D collision)
        {
            if (collision.tag == "RightBoundary")
            {
                isMoovingRight = false;
                Flip();
            }
            if (collision.tag == "LeftBoundary")
            {
                isMoovingRight = true;
                Flip();
            }
        }
    
        void Flip()
        {
            isFacingRight = !isFacingRight;
            Vector3 playerScale = playerModelTransform.localScale;
            playerScale.x *= -1;
            playerModelTransform.localScale = playerScale;
        }
    
    
    }
    
    
//скрипт №2

public class PlayerController : MonoBehaviour
{
    [SerializeField] private float jumpForce;
    [SerializeField] private float bigjumpForce; 
    private float moveInput;

    private bool isGrounded;
    public Transform groundCheck;
    public float checkRadius;
    public LayerMask whatsIsGround;

    private Rigidbody2D rigidbody;

    private int extraJumps;
    private int extraJumpsValue;


    void Start()
    {
        extraJumps = extraJumpsValue;
        rigidbody = GetComponent<Rigidbody2D>();

    }

    void FixedUpdate()
    {
        isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatsIsGround);

    }


    void Update()
    {
        if (isGrounded == true)
        {
            extraJumps = extraJumpsValue;
        }


        if (Input.GetKeyDown(KeyCode.Space) && extraJumps > 0 && rigidbody.velocity.y > 0)
        {

            rigidbody.velocity = Vector2.up * jumpForce;
            extraJumps--;

        }
        else if (Input.GetKeyDown(KeyCode.Space) && extraJumps == 0 && isGrounded == true && rigidbody.velocity.y == 0)
        {
            rigidbody.velocity = Vector2.up * jumpForce;
        }


        if (Input.GetKeyDown(KeyCode.W) && extraJumps > 0 && rigidbody.velocity.y > 0)
        {

            rigidbody.velocity = Vector2.up * bigjumpForce;
            extraJumps--;

        }
        else if (Input.GetKeyDown(KeyCode.W) && extraJumps == 0 && isGrounded == true && rigidbody.velocity.y == 0)
        {
            rigidbody.velocity = Vector2.up * bigjumpForce;
        }
      

    }

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-03-23
@freeExec

You start deleting lines in a running script until another one works. Perhaps there will be illumination.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question