using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Movement : MonoBehaviour
{
public GameObject leftLeg;
public GameObject rightLeg;
Rigidbody2D leftLegRB;
Rigidbody2D rightLegRB;
public Rigidbody2D rb;
public Animator anim;
[SerializeField] float maxspeed = 10f;
[SerializeField] float speed = 2f;
[SerializeField] float jumpHeight = 2f;
[SerializeField] float legWait = .5f;
private bool isOnGround;
public float positionRadius;
public LayerMask ground;
public Transform playerPos;
// Start is called before the first frame update
void Start()
{
leftLegRB = leftLeg.GetComponent<Rigidbody2D>();
rightLegRB = rightLeg.GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (Input.GetAxisRaw("Horizontal") != 0)
{
if (Input.GetAxis("Horizontal") > 0)
{
anim.Play("WalkLeft");
StartCoroutine(MoveRight(legWait));
}
else
{
anim.Play("WalkRight");
StartCoroutine(MoveLeft(legWait));
}
}
else
{
anim.Play("idle");
}
isOnGround = Physics2D.OverlapCircle(playerPos.position, positionRadius, ground);
if (isOnGround == true && Input.GetKeyDown(KeyCode.Space))
{
leftLegRB.AddForce(Vector2.up * (jumpHeight * 1000));
rightLegRB.AddForce(Vector2.up * (jumpHeight * 1000));
}
}
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);
if (rightLegRB.velocity.x > maxspeed)
rightLegRB.AddForce(Vector2.right * (speed / 10) * Time.deltaTime , ForceMode2D.Impulse);
}
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);
if (leftLegRB.velocity.x > maxspeed)
leftLegRB.AddForce(Vector2.left * (speed / 10) * Time.deltaTime , ForceMode2D.Impulse);
}
}