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 13yearoldceo · Apr 13, 2014 at 02:51 PM · 2dcollisiontriggersgenerator

My trigger is not working right

hello there i am making a video game that require a obstacle generator similar to the one in flappy birds when the character goes though the trigger located in the center of the obstacles it is suppose to spawn another obstacle a few units up one at a time but it spawns a whole bunch in the air which slows down my computer can you guys help me make it spawn one at a time thx here is my script

pragma strict

var max : float = 5; var min : float = 0; var obstacle : GameObject; var regenerate : boolean = false; var far : float = 0; var yes : boolean = true;

function Start () {

}

function Update () {

var x = Random.Range ( min, max);

var y = far + transform.position.y;

if(regenerate){

Instantiate( obstacle, Vector3(x,y,0),Quaternion.identity); regenerate = (false);

}

}

function OnTriggerEnter2D (other: Collider2D) { { regenerate = (true);}

}

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

1 Reply

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

Answer by keburanuil · Apr 14, 2014 at 01:47 PM

  1. Add Box Collider 2D to your obstacle that contains your script. Enable Is Trigger in this box collider.

  2. Add Box Collider 2D and Rigidbody 2D to your character.

  3. (Optional) Change gravity scale value to 0 in rigidbody if you don't need Unity physics engine.

By the way you don't need the update function/loop in your script. Now it calculates random number and uses transform.position.y on every frame. Use Regenerate function call when "other" enters into trigger 2D.

 var max : float = 5;
 var min : float = 0;
 
 var obstacle : GameObject;
 
 var far : float;
 
 var x : int;
 var y : int;
 
 function Regenerate () {
 
 x = Random.Range (min, max);
 
 y = far + transform.position.y;
 
 Instantiate(obstacle, Vector3(x,y,0),Quaternion.identity); 
 
 }
 
 function OnTriggerEnter2D (other: Collider2D) {
     Regenerate();
 }

 

 
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 13yearoldceo · Apr 14, 2014 at 09:57 PM 0
Share

thx i already solved d problem tho with the method u used

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

22 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

Related Questions

2D Triggers and Collisions not working as expected. 2 Answers

OnTriggerExit2D not working, but OnTriggerEnter2D does??? 5 Answers

OnTriggerEnter2D doesn't work on collider with effector 0 Answers

OnTriggerStay2D only detecting trigger collisions while moving 3 Answers

1 collider 2 fuctions, how? 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