- Home /
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??
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.
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.
Whats its you definition of realistic ? Can you explain deeper what you trying to do ?
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.