- Home /
get event when i touch objects, "an animal for example"
i try to get animated voice when i touch my object (animal), i try this code, it run correctly when i use a cube object from unity, but when i try this code with 3D object that i downloaded , i dont get the voice. script.
//
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Sound : MonoBehaviour { public AudioClip[] aClips; public AudioSource MyAudio;
string Animal;
// Start is called before the first frame update
void Start()
{
MyAudio = GetComponent<AudioSource>();
}
// Update is called once per frame
void Update()
{
if((Input.touchCount > 0) && (Input.touches[0].phase == TouchPhase.Began))
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
//Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit Hit;
if(Physics.Raycast(ray, out Hit))
{
Animal = Hit.transform.name;
switch (Animal)
{
case "a1":
MyAudio.clip = aClips[0];
MyAudio.Play();
break;
case "a2":
MyAudio.clip = aClips[1];
MyAudio.Play();
break;
case "a3":
MyAudio.clip = aClips[2];
MyAudio.Play();
break;
/*case "Lion":
MyAudio.clip = aClips[0];
MyAudio.Play();
break;*/
default:
print("0000");
break;
}
}
}
}
}
Do your animals have colliders (primitive or mesh colliders)?
Hello @DjamelEnidde,
RayCast returns "bool True if the ray intersects with a Collider, otherwise false."
By default Unity Cubes are created with a cube collider. Did you check you have colliders attached to your gameobjects?
Answer by camdenlink7 · Jun 17, 2019 at 02:30 PM
Make sure the object (or animal in this case) has a collider on it and that the names are indeed the same as the code.
Your answer
Follow this Question
Related Questions
Touch android iput shootting 3 Answers
Sometimes, a touch can not be determined 0 Answers
GUITexture touch play animation 0 Answers
Small question about converting from Android to iOS 1 Answer