The problem is that my enemy falls too slowly until he start to attack me.
Here’s the code I’m using for movement
if (Vector2.Distance(player.position, rb.position) <= attackRange)
{
if (!isAttacking)
{
isAttacking = true;
rb.linearVelocity = Vector2.zero;
animator.SetTrigger("Attack");
}
}
else
{
isAttacking = false;
Vector2 target = new Vector2(player.position.x, rb.position.y);
Vector2 newPos = Vector2.MoveTowards(rb.position, target, speed * Time.fixedDeltaTime);
rb.MovePosition(newPos);
}
}
gravity doesn’t change, he’s just falling very slowly
I’m very bad at programming and can’t find the real cause of this problem
and maybe it can help
code for Attack
public int attackDamage = 20;
public Vector3 attackOffset;
public float attackRange = 1f;
public LayerMask attackMask;
public bool isFlipped;
public void Attack()
{
Vector3 pos = transform.position;
if (isFlipped)
{
pos -= transform.right * attackOffset.x;
}
else
{
pos += transform.right * attackOffset.x;
}
pos += transform.up * attackOffset.y;
Collider2D colInfo = Physics2D.OverlapCircle(pos, attackRange, attackMask);
if (colInfo != null)
{
colInfo.GetComponent<PlayerHealth>().TakeDamage(attackDamage);
}
}
void OnDrawGizmosSelected()
{
Vector3 pos = transform.position;
if (isFlipped)
{
pos -= transform.right * attackOffset.x;
}
else
{
pos += transform.right * attackOffset.x;
}
pos += transform.up * attackOffset.y;
Gizmos.DrawWireSphere(pos, attackRange);
}
code for patrolle
public GameObject pointA;
public GameObject pointB;
private Rigidbody2D rb;
private Animator anim;
private Transform currentPoint;
public float speed = 2f;
public float switchDistance = 0.6f;
public bool isFlipped = false;
void Start()
{
rb = GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
currentPoint = pointB.transform;
anim.SetBool("IsPatrolling", true);
}
void FixedUpdate()
{
Vector2 direction = (currentPoint.position - transform.position).normalized;
rb.linearVelocity = new Vector2(direction.x * speed, rb.linearVelocity.y);
float distanceToTarget = Vector2.Distance(transform.position, currentPoint.position);
if (distanceToTarget < switchDistance)
{
SwitchTarget();
Flip();
}
}
void SwitchTarget()
{
if (currentPoint == pointB.transform)
{
currentPoint = pointA.transform;
}
else
{
currentPoint = pointB.transform;
}
}
void Flip()
{
Vector3 flipped = transform.localScale;
flipped.x *= -1;
transform.localScale = flipped;
isFlipped = !isFlipped;
}
private void OnDrawGizmos()
{
Gizmos.DrawWireSphere(pointA.transform.position, 0.5f);
Gizmos.DrawWireSphere(pointB.transform.position, 0.5f);
Gizmos.DrawLine(pointA.transform.position, pointB.transform.position);
}
and another code for look at player things
public Transform player;
public bool isFlipped = false;
public bool playerInZone = false;
private Animator animator;
private bool playerWasInZone = false;
public Testenemypatrolle patrolScript;
private void OnTriggerEnter2D(Collider2D trig)
{
if (trig.CompareTag("Player") && !playerWasInZone)
{
playerInZone = true;
playerWasInZone = true;
animator.SetBool("PlayerInZone", playerInZone);
}
}
void Awake() {
animator = GetComponent<Animator>();
}
public testenemyweapon enemyWeapon;
public void LookAtPlayer()
{
if (!playerInZone) return;
Vector3 flipped = transform.localScale;
if (transform.position.x > player.position.x && !isFlipped)
{
flipped.x = -Mathf.Abs(flipped.x);
transform.localScale = flipped;
isFlipped = true;
}
else if (transform.position.x < player.position.x && isFlipped)
{
flipped.x = Mathf.Abs(flipped.x);
transform.localScale = flipped;
isFlipped = false;
}
if (enemyWeapon != null)
{
enemyWeapon.isFlipped = isFlipped;
}
}
and full te_run code
public float speed = 2.5f;
public float attackRange = 3f;
private Testenemypatrolle patrolScript;
Transform player;
Rigidbody2D rb;
testenemy testenemy;
bool isAttacking = false;
override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
player = GameObject.FindGameObjectWithTag("Player").transform;
rb = animator.GetComponent<Rigidbody2D>();
testenemy = animator.GetComponent<testenemy>();
isAttacking = false;
patrolScript = animator.GetComponent<Testenemypatrolle>();
}
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (testenemy.playerInZone)
{
patrolScript.enabled = false;
}
if (!testenemy.playerInZone)
{
return;
}
testenemy.LookAtPlayer();
if (Vector2.Distance(player.position, rb.position) <= attackRange)
{
if (!isAttacking)
{
isAttacking = true;
rb.linearVelocity = Vector2.zero;
animator.SetTrigger("Attack");
}
}
else
{
isAttacking = false;
Vector2 target = new Vector2(player.position.x, rb.position.y);
Vector2 newPos = Vector2.MoveTowards(rb.position, target, speed * Time.fixedDeltaTime);
rb.MovePosition(newPos);
}
}
override public void OnStateExit(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
animator.ResetTrigger("Attack");
isAttacking = false;
}