Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by voporak5 · Aug 04, 2013 at 04:56 PM · gameobjectinstantiate

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?

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image youngapprentice · Aug 04, 2013 at 05:04 PM 0
Share

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.

avatar image voporak5 · Aug 04, 2013 at 05:10 PM 0
Share

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.

avatar image voporak5 · Aug 04, 2013 at 05:34 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DFLY · Dec 04, 2013 at 01:20 PM 0
Share

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.

avatar image orihq · Jun 20, 2018 at 07:57 PM 0
Share

Thank you :)

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges