Hi guys,
I know this is probably obvious though i cant seem to work it out.
i have a treasure chest with animation on it, when i click on the chest with the mouse, i want unity to play that animation.
This is the code i have though i cant seem to get it working…
using UnityEngine;
using System.Collections;
public class ClickMesh : MonoBehaviour {
private string getName;
// Use this for initialization
void Start () {
}
void Update(){
if (Input.GetMouseButtonDown(0)){ // if left button pressed...
Ray ray = GetComponent<Camera>().ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit)){
getName = hit.collider.name;
Debug.Log("Mouse Down Hit the following object: " + hit.collider.name);
getName.GetComponent<Animation>().Play("open");
}
}
}
}
any help would be great :)
Do you get the "Mouse Down..." message confirming hit is on chest? Does chest have a "open" animation (spelling/capitalization exact match )? Do you get an error?
– getyour411Does it have a collider to be hit by the Raycast?
– seth_slaxhi guys, so this is the error im getting: Assets/Mouse Events/ClickMesh.cs(16,8): error CS0029: Cannot implicitly convert type
– FlashXstring' toUnityEngine.GameObject' I have an animation called "open" (all lower case) and yes the console does trace out that i have clicked the object. I also have a box collider attached too, i just cant figure out the syntax to get the animation to play :( cheersit means that you should do it in 3D but that's just my opinion because my workflow is in 3d and i think it would be easier and better
– a161803398874