- Home /
Instantiate at Location Problem
void Update()
{
if(Input.GetKey(KeyCode.Space))
{
fireCounter += Time.fixedDeltaTime;
if(fireCounter >= fireRate)
{
fireCounter = 0;
MachineGun();
}
}
}
void MachineGun()
{
Instantiate(fastProjectile,gun.transform.position,gun.transform.rotation);
}
This is my code I have. Whenever I hold space bar my object sometimes gets instantiated at the object location "gun" and sometimes it instantiates 2 units below the object location "gun." I don't understand why because all I have for gun is this tiny cube without any colliders so it's not as if the "fastProjectile" is being pushed by the gun to 2 units below. My bullet is a tiny cube so I don't think it has anything to do with that either.
Any suggestions?
We'll need more information. It could have something to do with rigidbodies and colliders. It could have something to do with the transform of the Gun (parenting, etc ), and it could also have something to do with the object the code is attached to.
I don't know what else to give.
using UnityEngine;
using System.Collections;
public class OtherTest : $$anonymous$$onoBehaviour {
public Transform fastProjectile;
public Transform heavyProjectile;
public float fireRate;
public float fireRateCannon;
public float fireCounter;
public float fireCannonCounter;
public GameObject gun;
public bool uPress;
public bool yPress;
// Update is called once per frame
void Update () {
if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space))
{
fireCounter += Time.fixedDeltaTime;
if(fireCounter >= fireRate)
{
fireCounter = 0;
$$anonymous$$achineGun();
}
}
}
void $$anonymous$$achineGun()
{
Instantiate(fastProjectile,gun.transform.position,gun.transform.rotation);
}
}
That's the exact code
The script is atatched to a cube with a character controller, and rigidbody. When I hold spacebar down, my prefab starts getting spawned at the gun location which is just another cube with its box collider turned off. And my prefab is just another cube with
{
public float speed;
public int damage;
// Update is called once per frame
void FixedUpdate ()
{
transform.Translate(Vector3.forward * speed * Time.fixedDeltaTime);
}
}
attached to it.
The longer I hold space bar down, the farther down the location of the fastProjectile spawn point moves. The object that I want my things to spawn at isn't moving at all yet the true spawn point is.
Answer by voporak5 · Aug 04, 2013 at 05:41 PM
Ok, my problem was I didn't have my prefab centered at (0,0,0).
Yep. Just tried it and it works. That was exactly my problem. I deleted my prefab and started again making sure to create it at World coordinates 0,0,0. Now it instantiates to the right place.
Answer by RalphTrickey · Aug 04, 2013 at 05:41 PM
Your bullets are colliding with each other. You can have multiple updates between any fixedupdate. Slow down the rate of fire to at most one bullet per fixedDeltaTime, or better, either set a flag in FixedUpdate and clear that flag in Update or keep the lastBulletFired as a static and verify that it's transform is NOT the spawnpoint before allowing a firing.
http://answers.unity3d.com/questions/10993/whats-the-difference-between-update-and-fixedupdat.html
CORRECTION:Keep LastBullet a private variable, it doesn't have to be static.
Your answer

Follow this Question
Related Questions
[Solved]Instantiating prefab from a script and destroy it from another one 2 Answers
Get Component on Instantiated Object 1 Answer
Instantiate problem 1 Answer
Instantiate A Prefab 1 Answer
Make gameobject appear on collision 1 Answer