• 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
1
Question by Evil_Weevil · Aug 21, 2015 at 01:13 PM · timeprefabsaddforce

Distance between instantiated prefabs

alt text

I need a trail of prefabs which shows the directory of the flight of my projectile.

Here's my code:

 using UnityEngine;
 using System.Collections;
 
 public class bang : MonoBehaviour {
 
     public Rigidbody rb;
     public float jumpPressure;
     public bool canshoot;
     public bool canbomb;
     public Transform door;
     public GameObject booms;
     public float timer;
 
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody>();
         canshoot = false;
         canbomb = false;
 
     }
     
     // Update is called once per frame
     void Update () {
 
         if (Input.GetButtonUp ("Fire1")) 
         {
             
             rb.AddForce (transform.forward * jumpPressure * 2); 
             rb.AddForce (Vector3.up * jumpPressure);
             canshoot = true;
             canbomb = true;
             
             
         }
 
         if (timer > 0)
         {
             timer -= Time.deltaTime * 38f;
         }
         
         if (timer  <= 0 && canbomb)
         {
             Instantiate (booms, door.position, Quaternion.identity);
             timer = 2;
         }
         
 
     }
 
         void OnCollisionEnter(Collision other)
         {
             if (other.gameObject.CompareTag ("ground") && canshoot) {
                 rb.isKinematic = true;
             canbomb = false;
 
 
             }
     
     }
 }


As you can see I'm using a timer to determine the instantiation of my little spheres prefabs. I'm pretty happy with that, but how do set it so that there won't be any space between those little blue sphere? If make the timer too short, they start bumping into each other, and that's what I don't want.


Another question: can I somehow change the speed of the flight of my purple sphere? I was trying to slow time -- but it looks more like a lag, that's not what I need.

Thanks!

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by YoungDeveloper · Aug 21, 2015 at 01:19 PM

For blue spheres, you could simply check the distance using Vector3.Distance() between purple sphere and last sphere (or start point if first sphere doesn't exist).

if distance >= (blue sphere diameter + blue sphere radius + purple sphere radius) => spawn blue sphere at last sphere (or point) + blue sphere diameter and then set it as last.

This produce perfect results regardless or move speed.

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 Youri1er · Aug 21, 2015 at 01:33 PM 0
Share

Yes he is right

avatar image
1

Answer by Hellium · Aug 21, 2015 at 01:25 PM

I don't understand everything in your code (comments are cruely missing ! Think about it in the future !!) You may need to do some tweaks with the following code :

  using UnityEngine;
  using System.Collections;
  
  public class bang : MonoBehaviour {
  
     public Rigidbody rb;
     public float jumpPressure;
     public bool canshoot;
     public bool canbomb;
     public Transform door;
     public GameObject booms;
  
     // Adjust value here, or let the last line of the Start function do the job
     public float distance ;
     private Vector3 lastInstantiationPoint ;
  
     // Use this for initialization
     void Start () {
         rb = GetComponent<Rigidbody>();
         canshoot = false;
         canbomb = false;
        
        // Optionnal, you can adjust it, with a coefficient for example
        distance = 2 * booms.GetComponent<MeshRenderer>().bounds.size.x ;
     }
     
     // Update is called once per frame
     void Update () { 
         if (Input.GetButtonUp ("Fire1")) 
         {            
             rb.AddForce (transform.forward * jumpPressure * 2); 
             rb.AddForce (Vector3.up * jumpPressure);
             canshoot = true;
             Instantiate (booms, door.position, Quaternion.identity);
             lastInstantiationPoint = door.position ;
         }
  
         if( Vector3.Distance( lastInstantiationPoint, transform.position ) > distance && canbomb )
         {
             Instantiate (booms, door.position, Quaternion.identity);
             lastInstantiationPoint = door.position ;
         }
     }
  
     void OnCollisionEnter(Collision other)
     {
         if (other.gameObject.CompareTag ("ground") && canshoot) {
             rb.isKinematic = true;
             canbomb = false;
         }     
     }
  }
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 Youri1er · Aug 21, 2015 at 01:34 PM 0
Share

Hellium the best !

avatar image
0

Answer by Youri1er · Aug 21, 2015 at 01:32 PM

Your problem is : When you call Instantiate it take time and that time depend on your CPU usage.

Second problem is : Time.deltime is not equal all the time, it's normal but if your timer is equal to 0.00001 it will drecrease under 0 with Time.deltime, so it takes 2seconds + Time.deltatime. If your timer is equal to 0 + Time.deltatime so it will instantiate to 2seconds.

Third problem is windows, you dont know when the scheduler will give your processus the priority to run.

So for me there is only three method, use TrailRender(or Particle) of Unity. Or when you instantiate your sphere try to set his position at the last position sphere + his diameter. Or you disable collision between sphere and you instantiate mass.

But i prevent you, Instantiate is heavy performance so dont use the last solution. The best for me is TrailRenderer(or Particle)

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to randomly instantiate prefabs after a certain time? 2 Answers

How to use addforce after animation? 1 Answer

AddForce by Touch speed problem (Solved) 1 Answer

Please Help! Problems With AddRelativeForce in slow motion 1 Answer

GameObject instances affecting each other 0 Answers


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