My Prefab goes crazy and it moves faster when i click in another thing of the unity editor or the scene even if that didn't suppose to happen
I'm making a enemy who throws axes with a parabolic movement. The enemy create the axe,and the axe is programmed with the parabolic movement. However,this just works when i click the prefab and appears in the inspetor tab or the enemy.When i click,for example,on the background or anything ,it goes crazy,the prefab moves faster and the axe goes higher.
Here is the video who shows what happens.
This is the enemy's code:
public class Toaxe : MonoBehaviour
{ private Animator anim;
private Rigidbody2D rb;
public float Time_attack;
private float tiempo_de_ataque;
public Transform brazo_hacha;
public GameObject Hacha;
// Start is called before the first frame update
void Start()
{
rb=GetComponent<Rigidbody2D>();
anim = GetComponent<Animator>();
}
// Update is called once per frame
void Update()
{
if (tiempo_de_ataque <= 0)
{
anim.SetTrigger("Axe");
StartCoroutine(Axe());
tiempo_de_ataque = Time_attack;
}
else
tiempo_de_ataque -= Time.deltaTime;
}
IEnumerator Axe()
{
yield return new WaitForSeconds(0.25f);
Instantiate(Hacha, brazo_hacha.position, Hacha.transform.rotation);
}
}
And this is the weapon's code:
public class Toaxe_Axe : MonoBehaviour
{
public GameObject efecto_golpe;
public LayerMask ThePlayer;
public Transform Axe;
public float radio;
public float autodestruccion;
private float destruccion;
private Rigidbody2D rb;
//Movimiento parabolico
private float time;
public float angle;
private float gravity=9.8f;
public float velocity;
private float radialangle;
private float x;
private float y;
// Start is called before the first frame update
void Start()
{
rb = GetComponent<Rigidbody2D>();
time = 0f;
destruccion = autodestruccion;
radialangle = Mathf.Deg2Rad * angle;
}
void FixedUpdate()
{
rb.velocity = new Vector2(x, y);
}
// Update is called once per frame
void Update()
{
time += Time.deltaTime;
x = time*velocity*Mathf.Cos(angle);
y = y+ velocity*Mathf.Sin(angle)* time-(gravity*time*time)*0.5f;
if (destruccion <= 0)
{
Destroy(gameObject);
}
else
{
Collider2D[] hit = Physics2D.OverlapCircleAll(Axe.position, radio, ThePlayer);
foreach (Collider2D Player in hit)
{
Debug.Log("Ataca a " + Player.name);
Player.GetComponent<protobot>().Jugador_Atacado();
if (efecto_golpe != null)
{
Instantiate(efecto_golpe, transform.position, Quaternion.identity);
}
Destroy(gameObject);
}
destruccion -= Time.deltaTime;
}
}
}
Your answer
Follow this Question
Related Questions
GameObject not looking at me.. 1 Answer
I want to stop movement of character while i call animation like Kick Jump Punch etc.. Please help 1 Answer
instantiated prefab doesn't follow spawn rotation 1 Answer
Clamp transform.rotation? 0 Answers
Rotate transform to match another, but not past 180° 0 Answers