Play animation on target of raycast

I reckon the title says it all, I was messing around with a script that produces a raycast, and if the target that it hits is a rigidbody and has the right tag, then it is supposed to play an animation.

I had the code set up to just apply a force:

hit.rigidbody.AddRelativeForce(5,0,0);

That worked with no problem.

So I replaced:

hit.rigidbody.AddRelativeForce(5,0,0);

with:

other.animation.Play("test");

and got an error:

realModificationDate != timeStamp.modificationDate

I also tried:

animation.Play("test");
hit.animation.Play("test");

Is there a specific syntax I should use or something? I feel like I’m looking for my lost glasses that are sitting on my head here.

Of course the hit.animation line returns an error involving animation not being part of raycast, and the regular animation.Play just tries to play the “test” animation on the player (which I understand why on that).

Well, I actually got it to work with a change or two.

I just have one question remaining about the animation. I had this on a door, the door was set to start at 0degrees, rotate 90degrees and hold there a second or two, then rotate back to 0degrees.

It does this just fine, but when I press “Play” and run up to where the door is, it’s starting at 90degree position instead of 0degree, even though 0 is the start position. If I press my “open” key the door jumps to 0degrees, opens to 90 and holds a second, then closes back to 0 like it should.

My question is, why does it start at the open position and require me to play the animation once before it works properly?

Did you create the animation with the animation editor or with a 3D app?

I used the animation editor in Unity. The door is also a cube I made in Unity, then scaled to fit the doorway entirely as a test. I tend to model a proper door later with a texture and whatnot and use Unity’s editor to animate it.