- Home /
Macanim Mouse Buttons interaction c#
How can I make this work. I have set up 4 states in animator default being idle. I want the clicks to only work on this object...not on the others in the scene. So I figured TAGS? Please point me in the right direction. Thank you. Here's my code.
using UnityEngine;
using System.Collections;
public class A_BlueFishScript : MonoBehaviour
{
private Animator animator;
private int State = 0;
void Start ()
{
animator = this.GetComponent<Animator>();
animator.SetInteger ("State", 0);
}
void Update ()
{
if (Input.GetMouseButtonDown(0) && gameObject.tag == "A_BlueFish")
Debug.Log("Pressed left click for Rotate.");
animator.SetInteger ("State", 1);
if (Input.GetMouseButtonDown(1) && gameObject.tag == "A_BlueFish" )
Debug.Log("Pressed right click for Zoom.");
animator.SetInteger ("State", 2);
if (Input.GetMouseButtonDown(2) && gameObject.tag == "A_BlueFish")
Debug.Log("Pressed middle click for Move.");
animator.SetInteger ("State", 3);
}
}
State is an int(parameter) I set up in mecanim. Idle State = 0 and so on.
Have you remembered to change the tag of the GameObject? Based on your code, only objects that make use of the tag "A_BlueFish" should execute the said animation.
Yes I did. Thanks for the input. I am getting the debug messages on screen when I click. 1. But I get that when I click anywhere on the screen. 2. There is no interaction with the Object. It is as if the tag isn't working at all. Is there another way? Layer, Sorting layer or Raycast hit maybe? I forgot to mention. This is a 2D game. Thanks.
Have you set up transitions in your animator? If you get the debugs, animator.SetInteger is being called, so the problem is probably that you haven't set the transitions between the different states.
Also, the correct way to ensure that the correct object gets the inputs is to register input in a input manager script (only), and have the input manager send those inputs on to whatever needs to get that input.
Your answer
Follow this Question
Related Questions
NavMeshAgent animation - sliding 2 Answers
Not able to use a Vector as a condition in Mecanim? 2 Answers
Animator not registering changes in parameters. 0 Answers
How to get fileID information so i can just edit Mecanim Animator through script? 2 Answers
How can I know if the currentAnimState is mirrored or not? 0 Answers