Im trying to add a fireball to my 2d game, but its not working, can anyone help me?
My idea was creating a fireball, atach it to my Player, shoot it and destroy enemies.
This is what i did:
I created 2 box colliders, one for the fireball, one for my enemy, and in the enemy box i added this script:
Thanks! i think it works now!, ive tried it, but since i tagged my fireball as a Player, it disa`pears and respawns a nre Player, so i have 2 at the same time, still its an interesting effect :p, now ill try with a fireball with other tag, thanks again!
Another problem! my fireball gets destroyed by the enemies, i gave it a diferent tag, but maybe its because its a child of my Player, any help ? D:
Other problem, how can i get the x position form my player? but from a script atatched to another object,
pos = transform.position.x; this gives me the position of the object that got the script, but i want the position from my Player, how can i do that? thanks!
Shouldn’t the enemies be able to destroy the fireball after a player casts it? Maybe I’m missing something you’re trying to do here. I’m sure it’s something to do with it being a child of your player. I would make it a solo object just for that reason.
You can get the x position of the player 2 different ways. The first being setting up a public reference variable which you then drag and drop your player gameobject over top of in the inspector. The second being using GameObject.Find() and passing it your gameobject name. This would hold the reference to the player in a variable as well.
Awww its not working i disatached the fireball from my player, but it still gets destroyed by the enemy
Yeah, i already did the fireball movement, it fires, reach its maximum reach and it disapears, but if it touch the enemy first i want the enemy to get destroyed, not the fireball
Heres the enemy code:
function onTriggerEnter (other : Collider){
if(!stomp) {
if(other.tag == “Player”){
Destroy(other.gameObject);
var P : GameObject = Instantiate(Player, spawnPoint.position, Quaternion.identity);
var sf = Camera.main.GetComponent(SmoothFollow2);
sf.target = P.transform;
}
}
}
In the code says that the collider must be tagged as a player to get destroyed, so why it also destroys my fireball? D: i tagged my fireball as “Fire”, and added this script to the enemy:
My idea with this code was pressing F, fire the fireball, and at the end of its reach dissapears and move it to the original position, the only problem is that when i press f it only moves 1 step, so i need to press it repeatly, or hold f…
You should use a boolean variable to keep track of whether or not the fireball has been fired. That way when somebody presses ‘F’ you should set that boolean value to true, and then use the value of the boolean to determine if you should keep moving instead of putting that logic inside the input conditional code block.
I am assuming you want the following code to execute every time Update() is executed:
if(transform.position.x >= Xpos + maxAmount){
max = true;
}else if(transform.position.x <= Xpos){
max = false;
The way you are doing it now, this code only executes when ‘F’ is pressed which only happens once. Unless the user presses ‘F’ a second time, the code within the Input condition never executes again.
BTW: I would actually recommend instantiating a new fireball prefab every time ‘F’ is pressed and separate your input condition from the actual fireball itself. The script on the fireball really should just be responsible for moving it once it has been created, and perhaps self destructing after a set amount of time, not looking for input to fire itself. You could attach a script that looks for input and instantiates fireballs to an empty game object and make the game object a child of your player.