- Home /
Triggering an animation via distance
Hi there,
Please be aware that im a complete unity noob so please talk to me like an idiot.
I was wondering how i could trigger an animation on an object by coming into close proximity to that object. The animation along with the modeling has been done in 3ds max.
Use the Search feature to look for proximity, plenty of answers
What have you tried? Please use the search feature next time!
Answer by stingman · Apr 23, 2012 at 06:02 PM
Here's something to get you started. Attach this JavaScript to your player and mess around with it... the target is the object you want to have distance between your player. The detectRange is a variable to determine the distance between the player and the target at which the animation will be triggered.:
var target : Transform;
var dodecahedron : GameObject;
var detectRange: float = 30;
function Update() {
var tgtDirection = target.position - transform.position;
var tgtDistance = tgtDirection.magnitude;
if (tgtDistance <= detectRange) {
dodecahedron.animation.Play("NameOfYourObject'sAnimation");
}
}
Hope this helps you.
just a quick question - where abouts do I place my object into that script? The object in question named in unity as 'dodecahedron'
edited my answer with what you need. just drop your "Dodecahedron" game object into the variable slot in the inspector.
Have you done one of the tutorials before? $$anonymous$$ost of the time its the same code in c#
, you just have to change the way you set up variables!
Your answer
Follow this Question
Related Questions
trigger animation problems 2 Answers
Triggering a basic animation via Box Collider 4 Answers
Light animation and Main Menu animation at same time from 1 trigger 0 Answers
Multiple animation with One Trigger ?!! 1 Answer
Issues with Animator Controller - Trigger does not reset to false after being set with SetTrigger 2 Answers