Yes, I know it’s a total noob topic, but a swear I have been all over the scripting docs and the forums but I still cannot find an answer.
If I have multiples of the same object, how can I animate only the one I touch? Every script I have tried animates every object of the same type. I have tried Collider.tansform, Collider.gameObject, Collider.GetInstanceID(). Nothing works. Every instance of the character animates when I touch one of them. Here is the most recent code I have been using:
var hit : RaycastHit;
function Update () {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButtonDown (0)) //Returns true during the frame the user touches the object
{
if (Physics.Raycast (ray, hit, 200))
{
var me:Collider=hit.collider;
me.transform.gameObject.animation.CrossFade(“Walk1”);
}
}
}
Also, note I have had to rely on the mouse code since Raycast needs a Vector3 and iPhone.touch sends out a Vector2. How can I convert this?
Sorry for the “noobism” but I honestly cannot figure this out. Basically, I cannot get the ultimate parent object of a Collider since there is no Collider.parent command.
When I just access the transform for translation or rotation it works, like so…
transform.Rotate(0,10,0);
This will only rotate the object I touch, but when I try animation commads, every instance of the character animates. Someone out there must have done something similar to this already so I’m surprised I haven’t found anything similar yet.
I have also tried “transform.parent” and that did not work either. All the characters animate when I click on one of them. Please, does anyone know how to accomplish this?
OK. This code seems to be working. I now have to see if it will work in the actual game.
var hit : RaycastHit;
function Update () {
var ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Input.GetMouseButtonDown (0)) //Returns true during the frame the user touches the object
{
if (Physics.Raycast (ray, hit, 200))
{
var me:Collider=hit.collider;
me.transform.parent.animation.CrossFade(“Walk1”);
}
}
OK. This code seems to be working. I now have to see if it will work in the actual game. var hit : RaycastHit; function Update () { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); if (Input.GetMouseButtonDown (0)) //Returns true during the frame the user touches the object { if (Physics.Raycast (ray, hit, 200)) { var me:Collider=hit.collider; me.transform.parent.animation.CrossFade(“Walk1”); } } }
Just wondering if you got all this to work. I’m having the same issue.
As a test, I have two animations linked to two different buttons.
Touch either button and they both play.
Like you I’ve had difficulty getting a solid response from the forum.
So the key is the collider?
I was looking into LayerMask. I posted a couple questions in the forum but no good response came.
Thanks in advance for any insight you can send my way.
not sure if that it’S adequat for your need but for a shooter game also using many prefab character which I shoot at I also get that issue of having them all animating shooting only one, so i just put them in an array at begining and use an unique index for each prefab , corresponding to their order on the array, not pretty solution i assume but at least it work for my needs.