I am having an issue with Instantiate.
`
// ************************** Shoot Logic *********************************
// make sure we have ammo and the shoot is not on cooldown
if(Ammo > 0 && shootTimer <= 0)
{
// shoot timer is reset so it's ok to fire.
if(Input.GetButtonUp("Fire1"))
{
Ammo = -1;
shootTimer = 1.0f;
Vector2 direction; // direction the player is facing
Vector2 gunPos; // position of the Gun on the player
// which direction is the player travelling / facing?
if(Input.GetAxis(axisName) < 0)
{
// set the direction the bullet will travel to left
direction = -Vector2.right;
gunPos = new Vector2 (GameObject.FindGameObjectWithTag("Gun").transform.position.x - 1, // + 1 to stop the bullet from hitting the player
GameObject.FindGameObjectWithTag("Gun").transform.position.y);
}
else
{ // set the direction the bullet will travel to right
direction = Vector2.right;
gunPos = new Vector2 (GameObject.FindGameObjectWithTag("Gun").transform.position.x + 1, // + 1 to stop the bullet from hitting the player
GameObject.FindGameObjectWithTag("Gun").transform.position.y);
}
// set the bullet to "face" the same direction as the player
Quaternion quaternionDirection = Quaternion.LookRotation(direction);
// spawn the bullet
Instantiate(bullets,gunPos, quaternionDirection);
}
}
else
{
// bullet is on Cooldown so let's reflect the passage of time in the timer
shootTimer -= Time.deltaTime;
}
`
The issue is that bullets are not rendering in the Scene or Game views. When I drag the prefab into the scene it renders fine. The “bullet” is instantiating because “Bullet(Clone)” shows up in the Hierarchy. I also tried it with a standard cube mesh and that shows up just fine.
Script attached to bullet:
float speed = 10.0f;
void Update()
{
transform.position += transform.forward * speed * Time.deltaTime;
Destroy (this.gameObject, 20.0f); // destroy after 20 seconds just in case
}
Sprite Components:
- 2D BoxCollider
- Sprite Renderer (order in layer: 10)
- Script
In short, the sprite is not rendering when instantiated.
Any assistance would be greatly appreciated!
Thanks
EDIT 3/29/2014:
Upon further examination of this issue, Thanks again Key_Less, I noticed that the projectile is “flying off” at some weird angle when I paused the game the Bullet’s position was X: -6000 Y: -1100 Z: 0 and rotatation x: 0 y: 270 Z: 0. I’m certain the Quaternion is not “working correctly”, can anyone provide any insight into why this is?
The issue was that the sprite was being rotated 270 degree on the Y axis. I am posting this answer so that if anyone comes across a similar problem they can try ad troubleshoot the problem the same way