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 nasoukikos · Feb 23, 2014 at 05:57 PM · triggerpositionmath

Help with a script

So basicly i have an object with a sphere collider as a trigger. when the player enters the trigger the object acesses a script in the player and starts adding to it. What i want is the amount to be added to be relevant with the player's position towards the object. For example, if the player is 5m away add 10 every second, but if the player is 2m add 30 per second. how something like this could be written in code??

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by joseques · Feb 23, 2014 at 06:29 PM

Attach this with a script to the gameobject with the collider.

C#

 void OnTriggerStay(Collider col){
     if(Vector3.Distance(col.position, transform.position) < 5){
         col.gameObject.GetComponent<MyComponent>().myvar += 10;
     }
 }

You can store the instance of the component but that it's other story.

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 nasoukikos · Feb 23, 2014 at 06:49 PM 0
Share

i thought of an if statement but that would need a bunch of them to have a more realistic feel to it because it would be something like a response time for the player.

avatar image joseques · Feb 27, 2014 at 03:34 AM 0
Share

Whats its you definition of realistic ? Can you explain deeper what you trying to do ?

avatar image
0

Answer by RyanZimmerman87 · Feb 27, 2014 at 04:37 AM

I would just create a bool that determines whether you are in or out of the trigger.

So you would need an OnTriggerEnter and OnTriggerExit to toggle the bool to be true or false.

Then once it is true you could do a variety of things with the logic. I would recommend using a coroutine that occurs once per second since you mentioned the per second thing specifically.

So something like this:

 bool exampleBool;
 
 GameObject playerObject;
 
 
 void OnTriggerEnter(Collider triggerObject)
 {
 //logic to ensure it's correct triggerObject
 exampleBool = true;
 
 //set the playerObject for easy distance tracking
 
 playerObject = triggerObject;
 
 //start the once per second coroutine
 exampleTriggerFunction();
 }
 
 void OnTriggerExit(Collider triggerObject)
 {
 //logic to ensure it's correct triggerObject
 exampleBool = false;
 }
 
 void exampleTriggerFunction()
 {
 if (exampleBool == true)
 {
 StartCoroutine (exampleEnumerator(1));
 }
 }
 
 IEnumerator exampleEnumerator(float delayTimer)
 {
 yield return new WaitForSeconds(delayTimer);
 
 if (exampleBool == true)
 {
 //create a distance check variable
 float distanceCheck;
 
 distanceCheck = Vector3.Distance (transform.position, playerObject.transform.position);
 
 if (distanceCheck <= 2)
 {
 //add to player
 }
 
 else if (distanceCheck <= 5)
 {
 //add to player
 }
 
 //etc for all possible distances
 
 //then you call the exampleTriggerFunction
 //determines if you are in collider 
 //if you in collider starts next 1 second timer
 exampleTriggerFunction();
 }
 }
 
 

That's pretty much what I would do judging from your original post. You might need a few more variables though if you want the first trigger to happen immediately instead of waiting 1 second, and to ensure the player can't just exit and re-enter the trigger repeatedly if you do want the first trigger to add to the player instantly.

You may want a counter to ensure that only one possible set of coroutines can ever be going at once, if you aren't careful they can stack up for something like this if the player abused enter and exit the trigger very quickly.

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

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

Instantiate breaking at certain positions? 0 Answers

Make something arrive at position in exactly X seconds? 2 Answers

arc object's position using sin 1 Answer

Calcultate spawn position based on spawn volume's rotation 2 Answers

Get then Set position on X and Y 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