How can I fixe thes problem?

My players, when I finish making the enemy change his direction of sprite, the player then starts to go down the map when I begin the game

This is the player script:

public class PlayerMovementScript : MonoBehaviour
{
Vector2 moveIinput;
Rigidbody2D myRigidbody;
[SerializeField] float runSpeed = 3f;
[SerializeField] float jumpSpeed = 6f;
[SerializeField] float climpSpeed = 3f;
float myGravityAtStart;
Animator myAnimator;
CapsuleCollider2D myBodyCollider;
BoxCollider2D myFeetCollider;

void Start()
{
    myRigidbody = GetComponent<Rigidbody2D>();
    myAnimator = GetComponent<Animator>();
    myBodyCollider = GetComponent<CapsuleCollider2D>();
    myGravityAtStart = myRigidbody.gravityScale;
    myFeetCollider = GetComponent<BoxCollider2D>();
}
void Update()
{
    Run();
    FlipSprite();
    ClimpLadder();


}
void OnMove(InputValue value)
{
    moveIinput = value.Get<Vector2>();
    Debug.Log(moveIinput);
}

void Run()
{

    Vector2 playerVilosity = new Vector2(moveIinput.x * runSpeed, myRigidbody.velocity.y);
    myRigidbody.velocity = playerVilosity ;
    bool playerMovesHorezontal = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
    myAnimator.SetBool("isRuning", playerMovesHorezontal);
   
}
void FlipSprite()
{
    bool playerMovesHorezontal = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
    if (playerMovesHorezontal)
    {
        transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x), 1f); 

    }
    
}
void OnJump(InputValue value)
{
    if (!myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
    {
        return;
    }
    if (value.isPressed)
    {
        myRigidbody.velocity += new Vector2(0f, jumpSpeed);
    }
}
void ClimpLadder()
{

    if (!myFeetCollider.IsTouchingLayers(LayerMask.GetMask("Climping")))
    {
        myRigidbody.gravityScale = myGravityAtStart;
        myAnimator.SetBool("isClimping", false);
        return;
    }
   


    Vector2 climpingVilosity = new Vector2(myRigidbody.velocity.x, moveIinput.y * climpSpeed);
    myRigidbody.velocity = climpingVilosity;
    myRigidbody.gravityScale = 0f;
    myAnimator.SetBool("isClimping", true);

    bool playerMovesvirtecly = Mathf.Abs(myRigidbody.velocity.y) > Mathf.Epsilon;
    myAnimator.SetBool("isClimping", playerMovesvirtecly);
    
}

}

and this is the enemy script:
{
Rigidbody2D myRigidbody;
[SerializeField] float movmentSpeed = 1f;

void Start()
{
    myRigidbody = GetComponent<Rigidbody2D>();

}


void Update()
{
    myRigidbody.velocity = new Vector2(movmentSpeed, 0f);
}
private void OnTriggerExit2D(Collider2D other)
{
    movmentSpeed = -movmentSpeed;
    FlipEnemyFacing();
}
void FlipEnemyFacing()
{
    transform.localScale = new Vector2(-(Mathf.Sign(myRigidbody.velocity.x)), 1f);

}

}

Hi Amr,

Have you already checked the collider that gets used for the cinemachine? Make sure it is a trigger collider, not a ‘solid’ collider. Otherwise, it’ll push she player out.


See also:

Is there something with my code, my camera is completely fixed but my jump is broken, it only jumps once and then the foot collision doesn’t work

ok I fixed it but I cant control the player speed idnw & thanks for helping me with my camera

Where are you trying to change the character speed?

In the code, you have runSpeed and other speed values. The variables are declared with the [SerializeField] attribute. This means that the Inspector controls their values, not the code. Make sure to adjust the speed values in the PlayerMovementScript component in the Inspector.

Father down in your code, you have this: moveIinput.x * runSpeed, which is correct. If the player still does not change his speed, please log runSpeed and playerVilosity into your console. Maybe the values are correct but something else prevents the player from running faster/slower.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms