0

I am new to coding, so please go easy and try to make this as simple as possible. I am surprised that what I am trying to do appears to be so difficult.

I have two objects, A and B. They each have a different animation, 1 and 2. I want the user to click on A and watch animation 1, or to click on B and watch animation 2. I also want each object to play their relevant animations and then change to different scenes.

Can this be done just in the Animator window without any code using bools or triggers and transitions? (I have tried multiple ways and can't work it out.)

If it needs a code attached to objects A and B, I have a trigger animation script (attached below) but it only plays one of the animations and is just if the mouse button is clicked, not by clicking on object A or B.

I can successfully do what I need with a change scene script: objects A and B have the change scene script attached and two different scenes to change to. How is the animation I want to play different and seemingly so complicated to implement? I haven't needed to declare any gameobjects in that script. Perhaps I need to GetComponent and something to do with PlayAnimation, but I have searched and searched and am surprised no one else has had this issue, or perhaps I'm not asking in the correct way. There are many tutorials that tell you how to play one animation, but not multiple.

using UnityEngine;
using System.Collections;

/// <summary>
/// The TriggerAnimation class activates a transition whenever the Cardboard button is pressed (or the screen touched).
/// </summary>
public class TriggerAnimation : MonoBehaviour
{
    [Tooltip("The Animator component on this gameobject")]
    public Animator animator;
    [Tooltip("The name of the Animator trigger parameter")]
    public string triggerName;

    void Update()
    {
        // If the player pressed the cardboard button (or touched the screen), set the trigger parameter to active (until it has been used in a transition)
        if (Input.GetMouseButtonDown(0))
        {
            animator.SetTrigger(triggerName);
        }
    }
}

using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;

public class ChangeScene : MonoBehaviour
{
    public void GoToScene(string sceneName)
    {
        Debug.Log("I'm going to change scenes!");
        SceneManager.LoadScene(sceneName);
    }
}

Thank you so much in advance. I'm really stuck. I am trying to learn by following tutorials and using Sololearn, but I'm finding it very difficult to translate what I'm learning into things I want to do.

Here's the updated code:

using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;

/// <summary>
/// The TriggerAnimation class activates a transition whenever the Cardboard button is pressed (or the screen touched).
/// </summary>
public class TriggerAnimation : MonoBehaviour
{

    public Animator animatorVR;
    public Animator animatorAR;

    public void OnPointerClick(PointerEventData pointerEventData)
    {
        Debug.Log("Clicked: " + eventData.pointerCurrentRaycast.gameObject.name);
    }

    void Update()
    {
        // If the player pressed the cardboard button (or touched the screen), set the trigger parameter to active (until it has been used in a transition)
        //OnPointerClick;
        {
            animatorVR.SetTrigger(onPressVR);
            animatorAR.SetTrigger(onPressAR);
        }
    }
}

I know this doesn't work but am not sure where to go from here.

  • `Input.GetMouseButtonDown` detects mouse button press not mouse button press over an Object. See [this](https://stackoverflow.com/questions/41391708/how-to-detect-click-touch-events-on-ui-and-gameobjects/41392130#41392130) for how to detect mouse click on objects. Implement that into the code in your question. If you still have issues, update your code – Programmer Feb 20 '18 at 13:56
  • Thank you for taking the time to answer my question. Yes I will be using it for touch on mobile so I've changed it to OnPointerClick. – sasskiaduncan Feb 20 '18 at 15:22

1 Answers1

0

Detecting a click on an object

Input.GetMouseButtonDown only checks the current state of the button, without any respect as to where the mouse is pointing. You're executing it within the Update event method, which runs every single frame.

The easiest way to detect if you're clicking an object is to put a Collider component (of any shape) on the object and use the OnMouseDown event function.

As noted in the comments by Galandil, OnMouse methods shouldn't be used for touch controls.

Running animations for two different objects

The Animator component can control a tree of objects (the object with the component, but also its children). You can do what you want without any additional code if you put objects A and B under a common parent object with Animator. Otherwise, yeah, you'll have to reference the other object somehow, e.g put something like this on both objects:

[Tooltip("The Animator component on this gameobject")]
public Animator animator;
[Tooltip("The Animator component on the other gameobject")]
public Animator otherAnimator;

...

animator.SetTrigger(triggerName);
otherAnimator.SetTrigger(triggerName);
j4nw
  • 2,227
  • 11
  • 26
  • 2
    `OnMouseOver()` + `Input.GetMouseButtonDown(0)` = `OnMouseDown()` ;) - but I wouldn't use the `OnMouse` callbacks anymore, it's better to implement the interfaces linked by Programmer in his comment to the question. – Galandil Feb 20 '18 at 14:08
  • @Galandil cheers, I improved the answer - but why exactly are the `OnMouse` callbacks strictly worse than handling mouse events yourself, when you don't need to do anything fancy? They aren't obsolete, as far as I can see. – j4nw Feb 20 '18 at 14:18
  • 1
    They behave "erratically" when touch inputs are concerned, whereas the `I*Handler` interfaces work perfectly fine with both mouse and touch inputs. – Galandil Feb 20 '18 at 14:23
  • Thanks for helping, guys. @j4nw : I'll post my new code. – sasskiaduncan Feb 20 '18 at 15:23
  • @j4nw : this won't let me post my new code. Am I meant to actually give the Animator the same name in my scene? public Animator animatorVR; Am I meant to actually give the triggerName a name? animatorVR.SetTrigger(onPressVR); – sasskiaduncan Feb 20 '18 at 15:31
  • @sasskiaduncan you don't need to post new code, just edit your question with the updated code (and all other modifications you've done so far). – Galandil Feb 20 '18 at 17:02
  • Thanks for that heads up @Galandil! – sasskiaduncan Feb 20 '18 at 17:42