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 MegaStevenLP · Mar 02, 2013 at 11:30 AM · triggerrandomhorror

Random Trigger placement?

Im creating a little Horror Game. At one Point, there is a Trigger which plays a Sound. But the Game will gets Boring if the Player know the Location of this Trigger. How can do,that the Trigger is every restart at another Point???

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 Chronos-L · Mar 02, 2013 at 12:25 PM

There is at least 2 ways you can place a trigger at random position:

1. Random.Range()

We can use this when we need the trigger to be in a specific room, but not a specific spot.

 public class TriggerPlacement : MonoBehaviour {
    public Vector3 lowerlim, upperlim;
 
    void Start() {
       gameObject.transform.position = 
       new Vector3(
          Random.Range( lowerlim.x, upperlim.x ) + transform.position.x,
          Random.Range( lowerlim.y, upperlim.y ) + transform.position.y,
          Random.Range( lowerlim.z, upperlim.z ) + transform.position.z,
       );
    }
 }

We can attached this to the trigger, it will then be placed in a random location limited by the lowerlim and upperlim.

Pros :

  • Easy to code

Cons :

  • Doesn't take in account for rotation ( the area will always be a fixed rotation box )

  • Trigger may end up in inaccessible area

    2. Transform Array

We can use this when we do not want the trigger's position to be too random, or we need it to be at either one of few specific locations ( for example, it has to be located at one of the 3 doors )

 public class TriggerPlacement : MonoBehaviour {
    public Transform[] location;
    public Collider triggerObject;
 
    void Start() {
       collider.gameObject.transform.position = 
       location[ Random.Range(0, location.Length) ];
    }
 }

Pros :

  • More control on the in-game position of the trigger

Cons :

  • Harder to manage when you have a lots of possible location to place one trigger ( you may need to use custom Gizmos to help you manage these empty object in scene )

Comment
Add comment · Show 2 · 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 MegaStevenLP · Mar 02, 2013 at 12:47 PM 0
Share

the codes doesnt works

avatar image Chronos-L · Mar 03, 2013 at 12:44 AM 0
Share

Fix 2nd script:

 collider.gameObject.transform.position = 
       location[ Random.Range(0, location.Length) ].position;

When you say the codes doesnt works, you meant there are errors, or nothing happens at all?

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

10 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

Related Questions

Randomly spawn a trigger area 1 Answer

Jumpscare (Triggers and stuff) 1 Answer

How do you make a trig object? 0 Answers

triggering random animations with gui 1 Answer

Change light when in trigger and pressing "E" ? 2 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