I have tried once and again to make unity understand that the enemy is NOT colliding with the correct object, but, after many changes, the debug.log ("va") is still triggering even when the enemy is not colliding with the "muro".
Please, please, help me with this if you know the answer.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class enemy_2 : MonoBehaviour {
public ColliderDistance2D colldist;
public Collider2D squareCollider;
public float Delay;
public float timer;
public bool FacingRight = true;
public Vector2 direction;
public GameObject Player;
public GameObject pared;
public GameObject muro;
public int EnemySpeed;
public int XmoveDirection;
void Start ()
{
Delay = 3;
squareCollider = pared.GetComponent ();
muro = GameObject.FindWithTag ("muro");
pared = GameObject.FindWithTag ("pared");
}
void FixedUpdate ()
{
timer += Time.deltaTime;
if (timer > Delay)
{
colldist = gameObject.GetComponent ().Distance (squareCollider);
Vector3 direction = new Vector2 (XmoveDirection, 0.0f);
gameObject.GetComponent ().velocity = direction * EnemySpeed;
if (colldist.distance < 2 && FacingRight == false && pared)
{
Flip ();
}
if (colldist.distance < 2 && FacingRight == true && muro)
{
Debug.Log ("va");
InverseFlip ();
}
if (GetComponent().tag == "Player")
{
Destroy (Player.gameObject);
}
}
}
void Flip ()
{
FacingRight = !FacingRight;
Vector2 localScale = gameObject.transform.localScale;
localScale.x *= -1;
transform.localScale = localScale;
XmoveDirection = -1;
}
void InverseFlip()
{
FacingRight = FacingRight;
Vector2 localScale = gameObject.transform.localScale;
localScale.x *= -1;
transform.localScale = localScale;
XmoveDirection = 1;
}
}
↧