Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Rad226 · Nov 25, 2017 at 01:20 PM · instantiateobjects

How to move and leave trail of instantiated objects behind in defined intervals between them ?

Hello,

I need to set a trail of objects behind player at certain intervals between them. Imagine it like a vehicle that lays mines behind or setting a poles for a fence. There are some conditions:

  • creating objects is triggered by pressing a button (pres button -> start laying objects, release button -> stop laying objects)

  • instantiated objects must be at a certain distance from one another

  • created "poles" stay on scene and player can turn around and add more "poles" to the fance - added poles must also be created at certain distance from existing ones.

This is my solution so far (I used OnTriggerStay method in cooperation with CompareTag):

 public class PoleTrailInstantiation : MonoBehaviour
  *//variables....*

 public GameObject Pole; *// object that would be instantiated, contains a trigger set to certain radius that prevents object instantiation and is tagged "CreatedPole"*
 public Transform polePosition;
 
 public Transform CreatorObject; *//empty object inside of player where "poles" are instantiated*
 public GameObject Player;
 private bool insideTrigger = false; *//this will tell me if I left trigger and instantiation would be allowed*
    
 // Update is called once per frame
 void FixedUpdate() *// this works only in fixedUpdate*
 {       
     if ((Input.GetKey(KeyCode.Space) && isInsideTrigger == false))
     {            
         InstantiatePole(); *// function that creates objects - "poles"*
     }
    
    insideTrigger = false;   
     
 } //end of FixedUpdate

 private void OnTriggerStay(Collider other)
 {
     if (other.gameObject.CompareTag("CreatedPole")) *//compares if trigger player stays in belongs to "createdPole" object)*
     {
         isInsideTrigger = true;
         print("OnTriggerStay activated!");
     }
 }  

And it actually works with one exeption. When player sets a trail of objects, stops creating them, turns around and starts creating a new trail, when players get near previously created "poles" there is a gap in instantiated objects cause trigger fields of both "poles" (created first and that are created now) overlap and constantly call OnTriggerStay method whis in my solution sets insideTrigger bool to true which prevents object instantiation :(

Any idea how can I get around that ? Perfection is so near but I just can't get to it.

Comment
Add comment · Show 4
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 Cherno · Nov 25, 2017 at 05:53 PM 1
Share

To circumvent this logic problem, it would be feasible to ins$$anonymous$$d handle it without triggers (which is good for performance, too). For example, you could store the position of the player when he drops an object, and as long as his current position (after some movement) is below a certain threshold, not drop a new object. Once the threshold is reached, drop a new object, and store the current position again. This would prevent dropping any object near the last one only.

avatar image Rad226 · Nov 25, 2017 at 06:23 PM 0
Share

I considered that. I also considered a timed instantiation. But it won't prevent instantiation of new objects near aldery existing. It could cause some objects to be abnormally close to one another. If object is instantiated no other object should be instantiated in a radius around it.

avatar image Cherno Rad226 · Nov 25, 2017 at 06:43 PM 0
Share

But... this is already handled by your code, no? I fail to see the problem.

avatar image Rad226 · Nov 26, 2017 at 05:03 PM 0
Share

I implemented solution based on distance traveled from last instantiated object, combined with trigger that prevents object instantiation in close vicinity of previously instantiated objects. Unfortunately the gap still occurs in certain situations. I think it is unavoidable cause no matter what there will allways occur a situation where player stays inside trigger and is at certain distance from last instantiated object and also is inside trigger of previously instantiated object. Thank You for suggestion. It helped to trimm the problem down a bit (and i learned few new things to ! :) ).

0 Replies

· Add your reply
  • Sort: 

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

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

Related Questions

Checking if object intersects? 1 Answer

setting variable in game object with instantiated object reference 2 Answers

FPS instantiate objects 1 Answer

Using Instance Nodes, worth it? 1 Answer

Instantiated objects are not destroying 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