I have a script attached to an object called Raty, when i instantiate the first prefab it works well, but the second doesnt execute the function JumpAttack correctly, the function only set the enemy speed to 0 but the other part of the fuction doesnt work.
public class Raty : MonoBehaviour
{
private Animator anim;
private EnemyController enemyController;
private Rigidbody2D rb;
private PlayerController playerController;
private bool attack;
private float xRotation;
private SpriteRenderer spr;
private void Awake() {
anim = GetComponent<Animator>();
enemyController = GameObject.FindGameObjectWithTag("EnemyController").GetComponent<EnemyController>();
rb = GetComponent<Rigidbody2D>();
playerController = GameObject.FindGameObjectWithTag("Player").GetComponent<PlayerController>();
xRotation = transform.rotation.x;
spr = GetComponent<SpriteRenderer>();
}
private void LateUpdate() {
MoveHorizontally();
JumpAttack();
AttackTemporal();
}
private void OnTriggerEnter2D(Collider2D collision) {
if (collision.CompareTag("AttackRaty")) {
attack = true;
}
}
public void MoveHorizontally() {
rb.velocity = Vector2.left * enemyController.enemies[1].enemySpeed;
}
public void Attack() {
playerController.playerHealth -= enemyController.enemies[1].enemyDamage;
}
public void JumpAttack() {
if (this.attack == true && xRotation <= 0) {
enemyController.enemies[1].enemySpeed = 0f;
Vector2 pos = transform.position;
Vector2 posTarget = new Vector2(-2.13f, -3.34f);
transform.position = Vector2.Lerp(pos, posTarget, 0.1f);
anim.SetBool("Jump", true);
Debug.Log("Raty");
} else if (this.attack == true && xRotation > 0) {
enemyController.enemies[1].enemySpeed = 0f;
Vector2 pos = transform.position;
Vector2 posTarget = new Vector2(-3.62f, 0.44f);
transform.position = Vector2.Lerp(pos, posTarget, 0.1f);
spr.flipX = true;
spr.flipY = true;
anim.SetBool("JumpUp", true);
Debug.Log("Raty");
}
}
Hello @SeberyInc,
It’s quite hard to see what’s causing the issue without other scripts nor AttackTemporal code however as your 2nd instantiated prefab has the same enemyController as the first instantiated prefab ;
If the 1st instantiated prefab does an “JumpAttack” it will set the 2nd instantiated prefab velocity to 0 since they use the same speed.
Other than that i’m sorry i can’t say much