- Home /
Something happen when object near player?
Hey, do you know how to make something happen when an object near the player? It's like a slender game. When slender near the player, static appear
The object is enemy
Put a collider on the object, make it a trigger, increase the size to whatever you'd like, check for the object's name in OnTriggerEnter()
.
That's not gonna work because the object is enemy. If I put a collider in it, and increase the size. Player would die suddenly with nothing happen.
Easiest way to do this is to add a script to the object with a reference to the player and check for the object being within certain radius of the player. In FixedUpdate() check for:
(transform.position - player.transform.position).magnitude < activationDistance
Where activationDistance defines the radius, or activation threshold. You can distance on an axis by only checking the x,y or z distance.
If you need something more accurate, for example something that takes into account the player and object shapes, then you'll probably want to use some type of trigger collider as mentioned by mattssonon.
I will try study Vector and $$anonymous$$agnitude. I don't learn it yet :P
Answer by drod7425 · Nov 06, 2013 at 03:19 PM
If you want the static effect from the slender game, you might want to check the distance and increase the static as the object gets closer to the player.
Vector3.Distance - http://docs.unity3d.com/Documentation/ScriptReference/Vector3.Distance.html
var other : Transform;
if (other) {
var dist = Vector3.Distance(other.position, transform.position);
print ("Distance to other: " + dist);
}
other : Transform << I should put enemy in here right?
Yes, I attach it to Player. Thanks. Wait, how do I manage the distance?
What do you mean? The Distance returns a float to the dist variable. Use that variable to increase/decrease your static. I don't know how you're doing that, so I can't really comment on that.
You might want to do some program$$anonymous$$g tutorials. The simplest way would be:
if(dist<static$$anonymous$$inimumDistance){
//start the static
}
Personally, I'd have a movie texture that played static and I'd change the texture's alpha inversely to the distance.
Your answer
