Пропадает цель для снаряда
Добавлено: 21 апр 2017, 21:37
В общем, у меня есть скрипты, это да.
Класс героя, от него наследуются все классы прочих героев (уже типа классы, как Маг, там, Воин, ну вы поняли), затем абилка и от нее наследуются все классы прочих абилок (опять же, огненный шар, или, не знаю, молния).
Вот есть скрипты:
Как все работает: вот есть герой, по нему кликают ПКМ, он становится активным. Затем кликают ЛКМ по цели, она устанавливается в target в HeroChooser'е, который затем переходит к герою, и затем - к абилке.
Затем, когда герой кастует абилку (то бишь просто прожимает циферку 1), появляется молния, но перед этим ей устанавливают цель и героя-создателя.
Только проблема в том, что герой-создатель остается, а цель просто исчезает, устанавливается на null. Если я вручную устанавливаю цель в рантайме, она по-прежнему тут же ставится на null.
Что не так?
P.S.: если в последнем скрипте закомментить target = null, то все еще не изменится ничего.
Класс героя, от него наследуются все классы прочих героев (уже типа классы, как Маг, там, Воин, ну вы поняли), затем абилка и от нее наследуются все классы прочих абилок (опять же, огненный шар, или, не знаю, молния).
Вот есть скрипты:
Синтаксис:
Используется csharp
public abstract class Ability : MonoBehaviour
{
public GameObject hero;
public float cooldown;
public int manacost;
public enum AbilityType
{
Target,
Area,
Effect
}
public abstract void onUse();
public abstract void SetTarget(GameObject _target);
public abstract void SetHero(GameObject _hero);
}
{
public GameObject hero;
public float cooldown;
public int manacost;
public enum AbilityType
{
Target,
Area,
Effect
}
public abstract void onUse();
public abstract void SetTarget(GameObject _target);
public abstract void SetHero(GameObject _hero);
}
Синтаксис:
Используется csharp
public class LightningBall : Ability
{
public float moveSpeed;
public int damage;
public int damageBonus;
public GameObject target;
private void Awake()
{
StartCoroutine(Reinforcing());
}
void Update()
{
if (target != null)
{
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, moveSpeed * Time.deltaTime);
}
}
IEnumerator Reinforcing()
{
while (true)
{
damage += damageBonus;
moveSpeed *= 1.1f;
yield return new WaitForSeconds(1.0f);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
collision.gameObject.GetComponent<Hero>().currentHealth -= damage;
DestroyObject(gameObject);
}
public override void onUse()
{
if (target != null)
{
print("123");
Instantiate(gameObject, hero.transform.position, Quaternion.id[syntax=csharp][/syntax]entity);
}
}
public override void SetTarget(GameObject _target)
{
target = _target;
}
public override void SetHero(GameObject _hero)
{
hero = _hero;
}
}
{
public float moveSpeed;
public int damage;
public int damageBonus;
public GameObject target;
private void Awake()
{
StartCoroutine(Reinforcing());
}
void Update()
{
if (target != null)
{
transform.position = Vector3.MoveTowards(transform.position, target.transform.position, moveSpeed * Time.deltaTime);
}
}
IEnumerator Reinforcing()
{
while (true)
{
damage += damageBonus;
moveSpeed *= 1.1f;
yield return new WaitForSeconds(1.0f);
}
}
private void OnCollisionEnter2D(Collision2D collision)
{
collision.gameObject.GetComponent<Hero>().currentHealth -= damage;
DestroyObject(gameObject);
}
public override void onUse()
{
if (target != null)
{
print("123");
Instantiate(gameObject, hero.transform.position, Quaternion.id[syntax=csharp][/syntax]entity);
}
}
public override void SetTarget(GameObject _target)
{
target = _target;
}
public override void SetHero(GameObject _hero)
{
hero = _hero;
}
}
Синтаксис:
Используется csharp
public class Hero : MonoBehaviour
{
public int maxHealth;
public int currentHealth;
public int maxMana;
public int currentMana;
public Ability[] abilities;
public bool canPlayThisHero;
public GameObject controller;
public float moveSpeed;
protected Vector2 speed;
public GameObject target;
public void dealDamage(int damage)
{
currentHealth -= damage;
}
public void heal(int heal)
{
currentHealth += heal;
}
private void Awake()
{
currentHealth = maxHealth;
currentMana = maxMana;
}
protected virtual void Update ()
{
if (controller == null)
{
controller = GameObject.FindGameObjectWithTag("GameController");
}
if (controller.GetComponent<HeroChooser>().currentHero == this)
{
speed.x = Input.GetAxisRaw("Horizontal") * moveSpeed;
speed.y = Input.GetAxisRaw("Vertical") * moveSpeed;
GetComponent<Rigidbody2D>().velocity = speed;
}
else
{
GetComponent<Rigidbody2D>().velocity = new Vector2();
}
target = controller.GetComponent<HeroChooser>().target;
}
}
{
public int maxHealth;
public int currentHealth;
public int maxMana;
public int currentMana;
public Ability[] abilities;
public bool canPlayThisHero;
public GameObject controller;
public float moveSpeed;
protected Vector2 speed;
public GameObject target;
public void dealDamage(int damage)
{
currentHealth -= damage;
}
public void heal(int heal)
{
currentHealth += heal;
}
private void Awake()
{
currentHealth = maxHealth;
currentMana = maxMana;
}
protected virtual void Update ()
{
if (controller == null)
{
controller = GameObject.FindGameObjectWithTag("GameController");
}
if (controller.GetComponent<HeroChooser>().currentHero == this)
{
speed.x = Input.GetAxisRaw("Horizontal") * moveSpeed;
speed.y = Input.GetAxisRaw("Vertical") * moveSpeed;
GetComponent<Rigidbody2D>().velocity = speed;
}
else
{
GetComponent<Rigidbody2D>().velocity = new Vector2();
}
target = controller.GetComponent<HeroChooser>().target;
}
}
Синтаксис:
Используется csharp
public class HeroChooser : MonoBehaviour
{
public Hero currentHero;
public GameObject target;
void Update ()
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector3.forward, 100f);
Debug.DrawRay(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector3.forward * 100f);
if (Input.GetMouseButtonDown(1))
{
if (hit.collider != null && hit.collider.GetComponent<Hero>() != null && hit.collider.GetComponent<Hero>().canPlayThisHero == true)
{
currentHero = hit.collider.GetComponent<Hero>();
}
else
{
currentHero = null;
}
}
if (currentHero != null)
{
if (Input.GetMouseButtonDown(0))
{
if (hit.collider != null && hit.collider.gameObject.layer == 8)
{
target = hit.collider.gameObject;
}
else
{
target = null;
}
}
}
}
}
{
public Hero currentHero;
public GameObject target;
void Update ()
{
RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector3.forward, 100f);
Debug.DrawRay(Camera.main.ScreenToWorldPoint(Input.mousePosition), Vector3.forward * 100f);
if (Input.GetMouseButtonDown(1))
{
if (hit.collider != null && hit.collider.GetComponent<Hero>() != null && hit.collider.GetComponent<Hero>().canPlayThisHero == true)
{
currentHero = hit.collider.GetComponent<Hero>();
}
else
{
currentHero = null;
}
}
if (currentHero != null)
{
if (Input.GetMouseButtonDown(0))
{
if (hit.collider != null && hit.collider.gameObject.layer == 8)
{
target = hit.collider.gameObject;
}
else
{
target = null;
}
}
}
}
}
Как все работает: вот есть герой, по нему кликают ПКМ, он становится активным. Затем кликают ЛКМ по цели, она устанавливается в target в HeroChooser'е, который затем переходит к герою, и затем - к абилке.
Затем, когда герой кастует абилку (то бишь просто прожимает циферку 1), появляется молния, но перед этим ей устанавливают цель и героя-создателя.
Только проблема в том, что герой-создатель остается, а цель просто исчезает, устанавливается на null. Если я вручную устанавливаю цель в рантайме, она по-прежнему тут же ставится на null.
Что не так?
P.S.: если в последнем скрипте закомментить target = null, то все еще не изменится ничего.