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 jeremy · Jun 14, 2012 at 04:01 PM · gameobjectraycastcollsion

How to instantiate gameobject on raycast collision.

Hi I need to know how to instantiate a gameobject on raycast collision. For example, I have a game that a robot shoots rockets at my player, but I don't want the robot to shoot the rockets till my player hits a raycast collider, so I need to know how to on a raycast collision,instantiate the rockets. And if you can I want the rockets to never sop instantiating. And if you can I would like it if the rockets instantiated every 5 seconds. Thanks so much.

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

Answer by aldonaletto · Jun 14, 2012 at 04:57 PM

I don't know what you mean by "raycast collider", but suppose you want to fire the rockets only when the player enters some area - is it correct? If so, you should use a trigger: create a cube, adjust its dimensions to cover the area, set the checkbox Is Trigger and then uncheck the Mesh Renderer to make the trigger invisible. Add this script to the trigger (ex-cube):

// script ZoneTrigger.js: var playerInside = false; // becomes true when the player is inside the zone

function OnTriggerEnter(other: Collider){ if (other.tag == "Player"){ // remember to tag the player as Player playerInside = true; } }

function OnTriggerExit(other: Collider){ if (other.tag == "Player"){ playerInside = false; } } Then place this code in your robot script:

var zoneScript: ZoneTrigger; // drag the trigger object here var shotInterval: float = 5; // shot interval in seconds private var nextShot: float = 0;

function Update(){ if (zoneScript.playerInside && Time.time > nextShot){ nextShot = Time.time + shotInterval; // place here the code to fire the rocket } }

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object Raycast...? 1 Answer

How to Destroy a gameobject on collision 3 Answers

How can I access other scripts and their functions? 3 Answers

C# GetComponent Issue 2 Answers

How to Get Gameobject position on screen resolution that not passed the resolution to set 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