- Home /
Hit any gameobject, animation will play?
Hi, I want to Play an animation if I hit a specific gameobject. But when I hit an other gameobject it will still play the animation. Here is the code (javascript) thats on the gameobject. Sorry I am just a beginner.
var particle : GameObject;
function Update () {
if (Input.GetMouseButtonDown(0)) {
// Construct a ray from the current mouse coordinates
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray)) {
// Create a particle if hit
animation.Play ("Animation");
}
}
}
Answer by Dave-Hampson · Feb 19, 2014 at 03:35 PM
Reading the documentation of Raycast, it looks like it returns true if it hits any collider: http://docs.unity3d.com/Documentation/ScriptReference/Physics.Raycast.html
So the reason this isn't working is that you don't have any code to check whether it's hitting the right game object.
Your answer
![](https://koobas.hobune.stream/wayback/20220613134029im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Why cant i get my animation to play? 0 Answers
How do I access the current playing animation clip of a gameObject? 0 Answers
Destroy GameObject A or B 1 Answer
What is wrong with my script? 1 Answer