- Home /
Triggering animation with raycasting does not work with my script for android AR app.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class flipping_ray : MonoBehaviour
{
public Animator anim;
string squareName;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
/* If the touch count is above 0, plus when the TouchPhase began.
Touch phase refers to the action the finger has taken on the most
recent frame update. Touch is tracked over "lifetime", as long as the
app is opening keep on tracking. TouchPhase is reported on each frame
they occured.
*/
if (Input.touchCount > 0 && Input.touches[0].phase == TouchPhase.Began)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
// Ray object = making a ray with camera/xyz points( (xyz points touching on screen) )
RaycastHit Hit;
// Structure used to get information back from a raycast.
// Now we have the Ray, and a structure that we can cast it to test a hit
// We call Physics.Raycast() and feed it the ray to cast it out into our 3D world
if (Physics.Raycast(ray, out Hit))
{
// Physics.RayCast returns:
// bool True if the ray intersects with a Collider, otherwise false.
squareName = Hit.transform.name;
if (squareName == "square_1")
{
anim.Play("frontToBack");
}
else anim.Play("none");
}
}
}
}
I am new to Unity and C#, but not to programming in general. I tried my best patching multiple relevant but incomplete tutorials together to form the above code. Basically, all I want to do is to scan a marker with Vuforia, make a 3D model appear, tap on the said model and trigger an animation with ray casting. If I can get this working then I can simply add another object to the scene and slowly scale up individual models and their corresponding animations. By the way, I have already managed to make animations play with recorded phone screen touch, but the animation plays for all 3D objects in the scene at the same time. This is something I want to avoid, which is why I am using the raycasting method. So can someone more experience please show me how to do this simple task correctly?
Your answer
Follow this Question
Related Questions
Cloud recognition in Vuforia 0 Answers
Jittery AR App, but only with Android 0 Answers
Playing video on a cube ? 1 Answer
Play AR Animation on TrackingFound 0 Answers
Is there a way to have Vuforia and ARCore in the same app? 4 Answers