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);
}
}