- Home /
Instantiated Object Spawning in different position
I am trying to spawn a fireball whenever the 'F' key is pressed, but every time I press the F key, the fireball spawns at a different place on the Y-axis. Any thoughts? Player Attack Script
using UnityEngine;
using System.Collections;
public class PlayerAttack : MonoBehaviour {
public float attackTimer;
public float coolDown;
public float damage;
public Rigidbody projectile;
public float speed = 0.1f;
Rigidbody fireball;
public Transform fireBallSpawn;
void Start () {
attackTimer = 1;
coolDown = 0.5f;
damage = 5.0f;
}
void Update () {
if (attackTimer > 0) {
attackTimer -= Time.deltaTime;
}
if (attackTimer < 0) {
attackTimer = 0;
}
if (Input.GetKeyUp(KeyCode.F))
{
if (attackTimer == 0) {
Attack();
attackTimer = coolDown;
}
}
}
private void Attack() {
fireball = Instantiate(projectile, fireBallSpawn.position, fireBallSpawn.rotation) as Rigidbody;
fireball.velocity += transform.forward * 5;
Destroy(fireball.gameObject, 2.0f);
}
}
At the bottom of the screen, the console is printing the y-axis for the spawner.
Answer by Eno-Khaon · Oct 20, 2015 at 12:00 AM
Nothing here appears to clearly suggest that the fireballs would be created in different locations each time, so my main two questions are:
1 - Does the fireBallSpawn position ever change in a way that's reflected by what you're experiencing
2 - Are the fireballs' colliders inside anything else when they're created and, if so, is that shoving them out quickly to another position immediately after spawning?
If it's neither of those, then I don't believe it would be feasible to provide a more accurate answer with the current information available.
It does seem to start reflecting a certain point on the y-axis after a couple shots. Any suggestions on how to fix it.
So I put a debug.log and I logged the position for the spawner on the x, y, and z axes and for some reason, the y value keeps changing in between about -1 and 1. I have no idea why this is happening. Any thoughts?
I'm afraid I have to grasp at straws a little bit. There are too many possibilities that I would have no idea about without more information (like pictures of hierarchy). So for now, I'll pester you for more information and offer food for thought through them.
Assu$$anonymous$$g you're launching the fireballs from your character or the like, what is the "fireBallSpawn" attached to as a child, if anything?
If the "fireBallSpawn" is a child of another object, does its localposition ever change?
For that matter, have your tests included looking around much, or is this facing a single direction?
How many objects, if there's a hierarchy, have colliders and/or rigidbodies?
Are these colliders interpenetrating? If so, is that shoving things apart which shouldn't be?
Are there any other scripts in your scene which might be influencing the motion of the spawner? For example, do any other scripts look for the spawner at all?
Answer by TheLazyTurtle9 · Oct 20, 2015 at 11:58 AM
I'm not completely sure as to what you mean, but you could try:
fireball = Instantiate(projectile, fireBallSpawn.position, fireBallSpawn.rotation) as Rigidbody;
fireball.AddForce (fireBallSpawn.forward * 5);
Destroy(fireball.gameObject, 2.0f);
the major thing that I changed was now it's working off the fireBallSpawn position instead of the transfom that may or may not be changing. the 5 in the AddForce part can be changed to velocity as well, but for Instantiate I prefer to use AddForce
Your answer
Follow this Question
Related Questions
Instantiated Object spawning at wrong position 3 Answers
Spawning objects on trigger does not work as expected 2 Answers
Having Trouble with Instantiating an object on an axis 2 Answers
Spawn: Instantiate GameObject 1 Answer
Y value wont work? 1 Answer