Raycast object click and animations

Hi all,

After setting up things today and fiddling with code, I’m at a loss atm.

What I’ve done is set up all animation clips from my FBX, and in the animator window set up the transitions, parameters and conditions.

So:

  • default base animation > Anim01 (condition: Anim01Run)
  • default base animation > Anim02(condition: Anim02Run)
  • default base animation > Anim03(condition: Anim03Run)
  • Anim04 (endposition Anim01) > default base animation (condition: Anim04Run)
  • Anim05 (endposition Anim02) > default base animation(condition: Anim05Run)
  • Anim06 (endposition Anim03) > default base animation(condition: Anim06Run)

Every transition has the boolean parameter set to true for the condition. First three transitions have the ‘Has Exit Time’ turned off.

Only three objects can be clickable, and have a box collider for the trigger.
The Raycast code for the boxes works, and gives me a proper result in the log.

code so far:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TouchCamAnims : MonoBehaviour
{

    private Animator animatorvar;

    //init
    void Start()
    {
        animatorvar = GetComponent<Animator>();
    }

    // Update is called once per frame
    void Update()
    {
        // Define mouse pointer position on click in scene & object collider
        if (Input.GetMouseButtonDown(0)) //0 refers to LMB
        {
            Ray mouseray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit rayhit;

            if (Physics.Raycast(mouseray, out rayhit, 100.0f))
            {
                //select collider & activate animation
                if (rayhit.transform.name == "cube1")
                {
                    rayhit collider// the code here to play ("condition parameter for transition clips", true/false?)
                   
                    Debug.Log("Clicked on mesh cube 1!");
                }

                if (rayhit.transform.name == "cube7")
                {
                    Debug.Log("Clicked on mesh cube 7!");
                }

                if (rayhit.transform.name == "cube13")
                {
                    Debug.Log("Clicked on mesh cube 13!"); // Play selected Animation Clip
                }

            }
        }
    }

                                            }

What I cant get to work, is the code for the raycast & trigger for the animation clip.
Also, the idea is to be able to click on the object(s) one time to run Anim01, than again to run Anim02 etc.
I’ve been looking at the docs and some YT videos today, and tried all variations on animation and such, but I might running in circles atm…

cheers for any tips, links or tricks.

rob

If you had a script on each of the clickable objects, you could store the current index/name of the animation.
A method on that script could be called when you hit it with a raycast.

Hopefully I understood your question correctly. :slight_smile:

Hi again :wink:

There’s no script on the objects.

Could you explain this a bit more, it sounds like this could be what I’m looking for?

Well, they have animators, right? Add a script on each that references their animator. Add a string array, an index and a method. Inside the method, play the animation by string from the array, adjust the index (make sure it doesn’t go out of bounds), and that’s it, I think?

Use GetComponent from the object hit by the raycast and call the method.

Thanks for that!

I’ll have a look at this tomorrow :slight_smile:

rob

Hi,

After some time away from this little project, I had some time to look into your suggestion.
It sounds a really simple solution for triggering simple state A/B animations.

I’ve got something, but not sure on the GetComponent bit.
The script works as far I can d&d the script on the object, and am able to set two clips and the recipient.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AnimSwitch : MonoBehaviour
{

    public AnimationClip[] SwitchClips;
    public Animation animator;

    private int index = 1;

    void Update()
    {
        //If the LMB is pressed, the collider tag will trigger the switch
        if (Input.GetMouseButtonDown(0))
        {
            Ray mouseray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit rayhit;

            if (Physics.Raycast(mouseray, out rayhit, 10.0f))
            {
                //select collider + object & activate animation
                if (rayhit.transform.tag == "LeftOBJ")
                {
                    if (SwitchClips.Length == 0) //Nothing happens.
                        return;

                   //When button is pressed down we increment up to the next animation clip
                    index += 1;

                   //On re-click it returns to the start clip.
                    if (index == SwitchClips.Length + 1)
                        index = 1;

                    print(index);//used for debugging

                     // how to finish this?
                     GetComponent [I]something something[/I] = SwitchClips[index - 1]; //This sets the clip index
                }
            }
        }
    }
}

thanks!

Rob

I’m also looking into using the AnimTrigger.SetBool(“clipname”, true) option for triggering both animations using the transition parameter in the animator module. But that’s another discussion :wink:

I was thinking of something else.

A script is on each game object, which their animator. When you raycast against a valid target, you can use GetComponent to get script and play whichever animation.
These individual scripts would keep track of their animations/index, unless that information was supposed to be shared.

Another option exists, though, and that’s using the IPointerClick interface, which would go in the script on each game object, and allow you to eliminate the raycast from a central script.

In either event you can use a SetTrigger if you want to call, rather than a bool which would be more suitable for a toggle type of animation (unless that’s what you want*).