- Home /
Found the problem in another script.
My chain hit goes to the center of the map, why?
So I'm making a chainhit spell which is suppose to shoot a projectile and if it hits an enemy the projectile will bounce to enemies close by. I used a tutorial for this (which might help). However upon casting the projectile goes towards (0, 0) instead of the direction it is suppose to go. The code I use to shoot the projectile is completely the same as other projectiles I shoot in my game, however it seems the prefab code is interacting with the direction but I'm not sure why. So this is the code I'm using:
private spellState currentSpellState;
private bool isCasting;
public Collider2D range1;
public Camera Viewer;
private Vector3 Target;
public GameObject Player;
public GameObject ChainBoltPrefab;
public float BulletSpeed;
public SpriteRenderer arcaneRange2;
private bool MouseOnRange;
private int manaValue;
private void OnMouseEnter()
{
if (gameObject.CompareTag("Player"))
{
MouseOnRange = true;
}
}
private void OnMouseExit()
{
if (gameObject.CompareTag("Player"))
{
MouseOnRange = false;
}
}
private void Start()
{
currentSpellState = spellState.notAttacking;
}
private void Update()
{
isCasting = ArcaneSpell.casting;
manaValue = Mana.mana;
StartCoroutine(ChainBoltCo());
}
private void ChainBolt(Vector2 direction, float rotationZ)
{
GameObject b = Instantiate(ChainBoltPrefab) as GameObject;
b.transform.position = Player.transform.position;
b.transform.rotation = Quaternion.Euler(0.0f, 0.0f, rotationZ);
b.GetComponent<Rigidbody2D>().velocity = direction * BulletSpeed;
}
private IEnumerator ChainBoltCo()
{
Vector3 difference = Target - Player.transform.position;
float rotationZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
if (Input.GetKey("2") && currentSpellState == spellState.notAttacking)
{
arcaneRange2.enabled = true;
if (Mana.isCasting == false && Input.GetKey("2") && MouseOnRange == true && currentSpellState == spellState.notAttacking && manaValue > 1 && manaValue < 99)//change mana amount
{
arcaneRange2.enabled = false;
currentSpellState = spellState.attacking;
Mana.isCasting = true;
//add cast animation
yield return new WaitForSeconds(1f); // casttime
float distance = difference.magnitude;
Vector2 direction = difference / distance;
direction.Normalize();
ChainBolt(direction, rotationZ);
// add shoot sound
yield return new WaitForSeconds(0.2f);
Mana.isCasting = false;
currentSpellState = spellState.notAttacking; // end of cooldown
}
Follow this Question
Related Questions
Why doesn't my prefab go to the closest enemy? 0 Answers
help with making enemy projectile fly at player at one speed 0 Answers
Multiple Cars not working 1 Answer
Destroying Prefab Also Destroys Prefab Reference 1 Answer
Distribute terrain in zones 3 Answers