First of all sorry for my english.
Im triyng to do kind of platform game with some storytelling and that.
Now im working in the AI system, i dont want the common patrol system, i want a Little more, but it is making me stress xd cause im new on this.
So, i do some sphere gizmos where if the player enters, the AI will folllow it unitl it goes out the sphere.
My problem is that i cant do it jump well. My idea was to put a collider that if it not detects ground, it jumps. Here is the AI script:
public class EnemyMovement : MonoBehaviour
{
public float lookRadius;
public Transform target;
public float speed = 2f;
public float maxSpeed;
public bool grounded;
public bool follow;
private GameManager gameManagement;
private Animator anim;
public static Rigidbody2D rb2d;
private void Start()
{
target = GameObject.FindGameObjectWithTag("Player").transform;
gameManagement = GameObject.Find("GameManager").GetComponent<GameManager>();
rb2d = GetComponent<Rigidbody2D>();
anim.SetBool("isFollowing", follow);
anim.SetBool("Grounded", grounded);
}
private void Update()
{
float distance = Vector3.Distance(target.transform.position, this.transform.position);
if (distance <= lookRadius)
{
follow = true;
if (grounded)
{
transform.position = Vector2.MoveTowards(new Vector2(transform.position.x, transform.position.y), target.position, speed * Time.deltaTime);
}
}else
{
follow = false;
}
}
private void FixedUpdate()
{
Vector3 fixedVelocity = rb2d.velocity;
fixedVelocity.x *= 0.90f;
if (grounded)
{
rb2d.velocity = fixedVelocity;
}
float limitedSpeed = Mathf.Clamp(rb2d.velocity.x, -maxSpeed, maxSpeed);
rb2d.velocity = new Vector2(limitedSpeed, rb2d.velocity.y);
}
private void OnCollisionStay2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
gameManagement.EnemyOnGroundStay();
}
}
private void OnCollisionExit2D(Collision2D collision)
{
if (collision.gameObject.tag == "Ground")
{
gameManagement.EnemyOnGroundExit();
}
}
private void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(transform.position, lookRadius);
}
}
And Here is the Script that tries to make it jump xd :
public class CheckJump : MonoBehaviour
{
public bool jump;
public float jumpPower;
private void FixedUpdate()
{
if (jump)
{
EnemyMovement.rb2d.AddForce(Vector2.up * jumpPower, ForceMode2D.Impulse);
jump = false;
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if(collision.gameObject.tag == “Ground”)
{
jump = true;
}
}
}
I supose that the problem is the physics, but i dont know
If someone could help me i will be grateful