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 Haerius · Feb 16, 2015 at 08:04 AM · instantiateparameter

Instanciate with parameter

I have a bullet prefab which I instantiate whenever the gun fires. The Bullet-Prefab has a script, which moves the bullet, and also a timer, which dictates for how long the bullet should travel. I do a raycast and pass the float how long the bullet should move to the gameobject. But the problem is, that The time it takes from instantiating the bullet, to bullet.GetComponent().startCount(time); is long, and the result is the bullet penetrating the wall. I would like to know if there is a way to instantiate the prefab with a parameter (in this case, the time) so there is no time delay between the instantiation and the passing the parameter.

This is how I move the bullet:

 using UnityEngine;
 using System.Collections;
 
 public class bullet : MonoBehaviour {
     public float speed = 80;
     private bool stop = false;
 
     // Update is called once per frame
     void Update () {
         if (!stop)
             transform.Translate (Vector3.forward * speed * Time.deltaTime);
     }
 
     public void startCount(float lifeTime){
         StartCoroutine(count(lifeTime));
     }
 
     IEnumerator count(float lifeTime){
         yield return new WaitForSeconds(lifeTime);
         stop = true;
     }
 }

Comment
Add comment · Show 2
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 Kiwasi · Feb 16, 2015 at 08:22 AM 0
Share

You have the wrong problem. You are doing something wrong with your time calculation, or misunderstanding the nature of discreet physics. Their may be a fence post error, or you are simply demanding too much of the physics engine. Either way this is not the problem.

Incidentally I've never seen this done before. Why not simply have the bullet keep flying until it enters a collision with the wall? Or am I missing the intricacies of this problem.

avatar image chariot · Feb 16, 2015 at 09:21 AM 0
Share

Im agree with Bored$$anonymous$$ormon.

Also

 using UnityEngine;
  using System.Collections;
  
  public class bullet : $$anonymous$$onoBehaviour {
      public GameObject urPrefab;
      public NameOfPrefabScript urScript;
  
      void Start () {
          urScript = urPrefab.GetComponent(NameOfPrefabScript);
      }
 
      // Update is called once per frame
      void Update () {
          urScript.variableThatNeedToChange = "ur changes here";
          Instantiate(urPrefab, transform.position, transform.rotation);
      }
  }

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by fafase · Feb 16, 2015 at 08:12 AM

I don't really get how the time between two lines can be too great since it is done in the same frame so the click counter does increase but you don't use that, while the frame time is the same for both method.

Now it is true that Instantiate is not the fastest, so you could reduce that overload wit reusing items, particularly for items like projectile that you reused a lot.

I would recommend a pool of object: http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling .

This way you are not creating but just activating/deactivating items.

Comment
Add comment · Show 1 · 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 Haerius · Feb 16, 2015 at 08:26 AM 0
Share

Ok I am going to look into that. Well I have set up a "slowmotion" mode, where I set the Time.timeScale to 0.1f. While activated, the bullet always stops on impact. But without the timescale, about 50% of the shots go through. This is why I suspect that the .getComponent is slowing things down.

avatar image
0

Answer by Kiwasi · Feb 16, 2015 at 08:23 AM

I'd almost bet on this being a fence post error. Your bullet is moving one physics step too far before the timer expires. Try subtracting the value of Time.deltaTime from your time value before you pass it in.

Comment
Add comment · Show 4 · 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 Haerius · Feb 16, 2015 at 08:34 AM 0
Share

I move the bullet inside the update function with transform.translate. I don't think that those are physics steps. However I am also having trouble fixing the bullethole to the surface of a moving physics object. There I am sure that the physics steps are messing things up, because my bullethole is sometimes fixed about one whole unit away from the actual object.... Any idea?

avatar image fafase · Feb 16, 2015 at 10:04 AM 0
Share

How do you actually check for collision? Because it could be a case of missed collision. Your bullet could be one frame entirely on one side and next frame entirely on the other side. Then no collision occurs. Solution, use linecast between previous and current position and see if it collides.

avatar image Haerius · Feb 16, 2015 at 11:22 AM 0
Share

The bullet is not a rigedbody. It is just an empty gameobject. I do a raycast and deter$$anonymous$$ the "collision" by just setting the lifetime according to distance/speed.

avatar image fafase · Feb 16, 2015 at 11:26 AM 0
Share

Would have top post the code but I think it is a different issue then and would deserve its own post.

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

21 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 avatar image avatar image

Related Questions

Checking if object intersects? 1 Answer

Instantiate prefab and passing parameter 1 Answer

find GUIText 1 Answer

How to instantiate prefab over the text one letter at a time? OR how to get transform of letters in a text 0 Answers

Instantiates 2 or more gameObjects on ButtonUp. 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