In this video I will show you how to work with ray-casting in AR. Hereby interacting with on screen 3D elements on your touch screen device.
Пікірлер: 134
@NGONeverGetOldPT5 жыл бұрын
Great video, thank you! The code for the future devs: using System.Collections; using System.Collections.Generic; using UnityEngine; public class soundCon : MonoBehaviour { public AudioClip[] aClips; public AudioSource myAudioSource; string btnName; // Use this for initialization void Start () { myAudioSource = GetComponent(); } // 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); RaycastHit Hit; if (Physics.Raycast(ray, out Hit)) { btnName = Hit.transform.name; switch (btnName) { case "myButton1": myAudioSource.clip = aClips[0]; myAudioSource.Play(); break; case "myButton2": myAudioSource.clip = aClips[1]; myAudioSource.Play(); break; case "myButton3": myAudioSource.clip = aClips[2]; myAudioSource.Play(); break; case "myButton4": myAudioSource.clip = aClips[3]; myAudioSource.Play(); break; case "myButton5": myAudioSource.clip = aClips[4]; myAudioSource.Play(); break; case "myButton6": myAudioSource.clip = aClips[5]; myAudioSource.Play(); break; default: break; } } } } }
@jiwachhetri41653 жыл бұрын
@Matthew Marshall I got scammed : (
@funjay95594 жыл бұрын
This is what I’ve been looking for for hours. OMG. thank you
@MsCoffeeFellow6 жыл бұрын
thank you so much for this tutorial @CubicBrain, it's so detailed but simple and functional as hell. you've really made my day.
@CubicBrain6 жыл бұрын
Emina Dizdarevic thanks a lot for the great feedback.
@freebacer4 жыл бұрын
Exactly what I was needing. Thankyou Very Much!!!!!!
@WhimsyCottage4 жыл бұрын
This helped me alot with a schoolproject. Thank you!
@deicide23242 жыл бұрын
For those who is not able to work with this code. You need to add a collider to the object, my suggestion is use the Mesh Collider and enable the Convex.
@master_zeec6 жыл бұрын
Wow, this is amazing, great work
@CubicBrain6 жыл бұрын
Thanks a lot. Glad you like it.
@nammonjv2 жыл бұрын
This video saved my life, thanks a lot 😭
@pauldouglas69316 жыл бұрын
Fantastic tutorial video. Thanks so much! ;)
@CubicBrain6 жыл бұрын
Thanks a lot Paul.
@pyrotaze6 жыл бұрын
Great tutorial! Keep up the good work :)
@CubicBrain6 жыл бұрын
Thanks a lot.
@joseph92273 жыл бұрын
Thank you very much for this tutorial :)
@adilahizzatihs96722 жыл бұрын
Thank you very much Sir for this tutorial
@3mr0y4 жыл бұрын
thanks a lot its very helpful for me
@Annefree982 жыл бұрын
Nice video!! Don't forget to remove your Main Camera, otherwise it gives the error that there are 2 audio listeners in the scene.
@alimmalik58464 жыл бұрын
Great video. can you help me? how the script is made if when cube is pressed, the object will appear and only focus on that object after cube is pressed
@slujan015 жыл бұрын
Work ;) Nice !!!!
@sameraswise32274 жыл бұрын
Thank you for this good video.. Can I add the object and audio in the same scene .. I mean when I but the card will appear the 3D object and audio or video
@benjamonrarira87045 жыл бұрын
hi can i get help with the android input or raycast mouse input plz
@tegarbrawijaya2 ай бұрын
does the button have to be inside the marker? or the markers function for mark only
@PEDRO40753 жыл бұрын
Hi! Can you help me on how to make different objects appear for each side of the Vuforia Multi-Image target? Imagine a multi-target cube, I want to place different objects for each face
@SsjDennis3 жыл бұрын
Hi, I hope you see this, since the vid. Is a bot older How can I code, so that when I click on an object, another Object (my UI panel) opens? + I want to open a different UI panel for each object I click on. Thanks for taking your time :)
@alashchanneltv33873 жыл бұрын
bro? How you does enable the touches with circles? I need to add this touch effects to my AR project. Please help me?
@BlunderBoyNehruJ4 жыл бұрын
actually i want make a pop out description in my AR app can anybody help me out with that thing eg when i'll touch on characters head it will give me description about like what is head how many veins that character ahs in that head all that stuff
@zilehumakhan83435 жыл бұрын
Hi, I am trying to make a virtual button which on click should show me the 3D plane in the Augmented Reality, It seems not be working for me. Someone, please help
@cebkun2 жыл бұрын
Great video ! Hi, how to setup using the mouse click?
@itsmezyvin5 жыл бұрын
Why the script not working in unity5? I confuse about calling btnName.
@bryanbarrios55355 жыл бұрын
Thank you!
@mehrdaddarafshi860 Жыл бұрын
Could u do the same process also for Area target objects? I wanna creat an AR game and want to add sound and animation script to different objects in my scene where I use vuforia Area targets. If yes how?
@vedantraopoly24005 жыл бұрын
How do I detect a raycast on a child object?
@giancarlomoran27303 жыл бұрын
Hey, can you show something like this, but when you click on the object, a video pops up and starts playing?
@1917revolution3 жыл бұрын
Hi Cubicbrain! I'm trying to do something similar, but opening a url when clicking a 3D object. I'm trying to mix this script with another one (at the end of the comment) seen on another video, but I've been unable till now. You know if there is any tutorial anywhere or, if it's something easy, could you give me a hint? Thank you so much! public class UrlOpener : MonoBehaviour { public string Url; public void Open() { Application.OpenURL(Url); } }
@billywdyt2 жыл бұрын
how about without image tracking or markerless ar?
@Jjolasman6 жыл бұрын
It works ;) thanks ;)
@Jjolasman6 жыл бұрын
However, the OnMouseDown function does the work too
@sudahmalam16 жыл бұрын
thanks dude,..
@AiniMazalan3 жыл бұрын
Thank you
@JosueAbraham6 жыл бұрын
How can I make a botón that stops the audio of other buttons and play other audio at same time ? the intention is to avoid that the sounds are not over lapping. tanks for the tutorial.
@CubicBrain6 жыл бұрын
Hi Josue, you can stop the individual audio sources, or stop all at the same time. the code is just Audio.Stop();, but if you want all to stoop, you have to identify them, stop them, and then play the next. the stop all script could look something like: void StopAllAudio() { allAudioSources = FindObjectsOfType(typeof(AudioSource)) as AudioSource[]; foreach( AudioSource audioS in allAudioSources) { audioS.Stop(); } }
@vivins32154 жыл бұрын
A clips is not showing for me
@MarioSasuke6265 жыл бұрын
Hi!, thank you so much bro, this video helped me so much but i have a question and i hope you can help me, the sounds that i used are longer so i need to stop them when tracking lost, so if u can help me with that i would apreciate it!
@CubicBrain5 жыл бұрын
Hi, thanks. You should be able to use the tracking lost function: private void OnTrackingLost() { audio.Stop();}
@anshuman19645 жыл бұрын
i am new to all this , just a quick question , is it possible to create a button in some space / in front of you (not on a specific spot or a picture ) . is it possible please help.
@CubicBrain5 жыл бұрын
Hi, Yes and no. Vuforia which I use here always needs a marker (picture) to track. this means that it will only function and activate if you have the pre-configured image/picture in front of the camera. if you want to track your surroundings without a marker, you would need ARCore, ARKit, og use Unitys AR foundation. this will track any flat surface.
@noelkonagai11006 жыл бұрын
Hi! I was wondering how you got the circle touch feedbacks? Was it a script?
@CubicBrain6 жыл бұрын
Hi Noel. Thats just my screen recorder making those.
@francesm25826 жыл бұрын
Hi! I wonder if is it possible to switch scene whenever I press the button? How can I do that? Thanks
@CubicBrain6 жыл бұрын
Hi Frances. It should be possible with the scene loader. try something like: SceneManager.LoadScene("OtherSceneName", LoadSceneMode.Additive);
@willy30766 жыл бұрын
Hi I'm trying this and I have some problem. I use Mapbox instead of Vuforia. Is it can't working? I do with your script, but I touched the objects on screen and it didn't respond. I also use the Debug to check the problem, but it stop at If before. How to solve this problem? Thank you so much.
@CubicBrain6 жыл бұрын
Hi, i'm not sure how mapbox handles the camera, but the approach should be kind of the same. at it's core it's just the raycasting from the camera that triggers the sound.
@JoshyHJoshy6 жыл бұрын
Hi there, Is it necessary to have the AudioSource variable as public and then drag & drop the ImageTarget game object onto its public field, so that the AudioSource component can be extracted and assigned to the slot? The AudioSource component is already attached to the ImageTarget game object, so why not just make it private and in the Start method, use GetComponent() in code? Would that not get the AudioSource component?
@CubicBrain6 жыл бұрын
Hi, strictly speaking no, You don't have to reference the AudioSource. However if the script was/is on another gameobject (like a gameController on an empty GameObject) it would be necessary.
@TheMobyh5 жыл бұрын
I get very high lag between when I touch a cube and when the sound plays (about a second) also when I press several cubes in quick succession there's audio issues
@diegoreyes38513 жыл бұрын
do u fix it?
@TheMobyh3 жыл бұрын
@@diegoreyes3851 I did, but I honestly can't remember how. I had help from a CS professor from my uni and it was over a year ago :( I don't even have the file anymore
@maadhavpatel26214 жыл бұрын
Hey , do you have idea about how to make ar app with groundplane detection for non-arcore devices
@CubicBrain4 жыл бұрын
Hi, I think AR foundation in Unity might have it. otherwise unitys project mars might work: unity.com/products/unity-mars
@seanmarkovic26545 жыл бұрын
Is it possible to make a button press open a URL? I've been trying to figure out how to do this but I can't find any information online regarding this on mobile devices
@CubicBrain5 жыл бұрын
should be possible with: public static void OpenURL(string url). docs.unity3d.com/ScriptReference/Application.OpenURL.html
@1917revolution3 жыл бұрын
Hi Sean! Were you able to do it? I'm trying to do just the same as you, but I'm not being able.
@bhavanidevi3264 жыл бұрын
I did all, the output came exactly but audio was not playing we I click the button help me??
@marthaatdarumatech53814 жыл бұрын
I have the same situation when I built it for an iOS device, but it works fine in a Android device. Try Android!
@muhammadtalhaali25414 жыл бұрын
I need help sir on how to open links with button click or object tap and dial mobile numbers :/
@simarpreetsingh0194 жыл бұрын
+1
@msahkoo6 жыл бұрын
i can't understand after 7:14, i build it for android phone but it doesn't work. please make detailed video after 7:14. I am beginner in AR.
@shrijaykesarwani67735 жыл бұрын
Hey bro i am also beginner
@xc58385 жыл бұрын
Hi can you please explain how to do raycasting with animation? Do I need to put myAnimationSource = GetComponent(); in void Start(), and then call case "object_1": myAnimationSource.Play("animation_001"); break; in this case?
@CubicBrain5 жыл бұрын
Hi Luke, I was just working on a project that uses the exact code from this example, but with animation instead of sound. yYou are completely correct, however you don't have to get the component in start, as you are just looking for generic hits, and checking for the name of the objects. so, a "public Animator anim;" to begin with to get the animator of the object you want to animate, and then just "case "myButton1": anim.Play("your animation name"); break;"
@xc58385 жыл бұрын
@@CubicBrain I posted my code below. Unfortunately, it does not work. I have followed your other tutorial for triggering animation with touch only which works great, but it triggers animations for all objects at once. This is why I want to use raycasting to trigger individual animations. Anyways thanks for the help. I really appreciate it. If you see something wrong with the code please tell me. I really want to get this working :D
@CubicBrain5 жыл бұрын
hi, Your code seem fine. Have you added the animator for the object you want to animate to the script in the inspector? also, make sure that all the names are correct (also capitalization)
@xc58385 жыл бұрын
@@CubicBrain i.imgur.com/ONXSmTC.png My current script is derived from your other tutorial with touch/keyboard animation but with no raycasting. Would it help if I try to get this working with keyboard input first?
@CubicBrain5 жыл бұрын
Hi Luke, If you want to hit a specific 3D object on screen (on a touch device) you need to use raycasting. otherwise it wont know where you are clicking. a canvas UI might not need raycasting, but then it would be stuck to your screen.
@wuimingleebarroga86295 жыл бұрын
hi, if i just want to touch an object to show a text above the object, may i know how to do it? How would the script be like? Can you help me ?
@wuimingleebarroga86295 жыл бұрын
using System.Collections; using System.Collections.Generic; using UnityEngine; public class clickObject : MonoBehaviour { public GameObject addText; void Update() { if (Input.GetMouseButtonDown(0)) { RaycastHit hit; Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); if (Physics.Raycast(ray, out hit, 100.0f)) { if (hit.transform.gameObject != null) { buttonClicked(hit.transform.gameObject); } } } } public void buttonClicked(GameObject gameObject) { addText.SetActive(true); // show 3D text } } Sadly it did not work when click on the AR object . Can you help me?
@rosannarifino93373 жыл бұрын
@@wuimingleebarroga8629 sorry, how did you do after?
@farahilyanah.16334 жыл бұрын
can we do this using unity personal?
@CubicBrain4 жыл бұрын
Yes you can.
@M3CH5OUL4 жыл бұрын
First off, these videos are great! I am learning a ton. I do have a question... Can anyone show me how to activate the animator triggers that belong to the other game objects? For example, if I press Button 1, Cube 1's "Roll" trigger is activated, and Cube 2&3's "Jump" triggers are activated.
@CubicBrain4 жыл бұрын
Hi, Thanks a lot. you can reference the other gameobjects Animator in your script. here is a script that might work for you: public class makeCubeSpin : MonoBehaviour { //reference the specific cube or gameobject in the inspector public Animator cubeAnimator; void Update() { if (Input.GetKeyDown("1")) { //Play the animation clip named "cubeAnimation" cubeAnimator.Play("cubeAnimation"); } } }
@M3CH5OUL4 жыл бұрын
@@CubicBrain Yes!!! Thank you so much! I'll post the end result once it's ready.
@warek1234567896 жыл бұрын
Hello, I have problem with this. I did everything, my scripts it's similar but when I want click nothing play. Can you help me? I have vuforia 7.2.20, when I click on the screen, nothing happens
@israelgallegos91765 жыл бұрын
i have the same problem :(, helppppp!
@warek1234567895 жыл бұрын
You need add to ARcamera Raycast :) I did this and is ok
@jmb53065 жыл бұрын
Hi, I have the same problem with Unity 2018.2.1.7f1 and Vuforia 7.5.26! ARcamera Raycast??? IAm a newbie, where to find this option or asset/Package? I did not find in Unity or Google Thx
@JoshuaMartinez-hu5bg6 жыл бұрын
Hello, I added the following code in the Update method to enable testing the buttons in the Editor, but apparently it is not possible to click on the buttons when the piano image is recognized and the buttons appear. Is it indeed the case or is there something missing in my code? #if Unity_EDITOR if ( Input.GetMouseButton(0) ) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if ( Physics.Raycast(ray, out hit) ) { _buttonName = hit.transform.name; switch (_buttonName) { case "Button1": _audioSource.clip = _KeysSoundClips[0]; _audioSource.Play(); break; case "Button2": _audioSource.clip = _KeysSoundClips[1]; _audioSource.Play(); break; case "Button3": _audioSource.clip = _KeysSoundClips[2]; _audioSource.Play(); break; default: break; } } } #endif
@CubicBrain6 жыл бұрын
Hi Joshua. I just tested your code in my project, and it works fine. (I just had to convert the naming to fit my project). Maybe the naming of your buttons is off? In regards to your test code here; This will disregard the "beginning of a mouse press, and just play over and over again if you hold the mouse button pressed (just a note). Here you have a c/p of my code: if (Input.GetMouseButton(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit hit; if (Physics.Raycast(ray, out hit)) { btnName = hit.transform.name; switch (btnName) { case "myButton1": myAudioSource.clip = aClips[0]; myAudioSource.Play(); Debug.Log("btn1"); break; case "myButton2": myAudioSource.clip = aClips[1]; myAudioSource.Play(); Debug.Log("btn2"); break; case "myButton3": myAudioSource.clip = aClips[2]; myAudioSource.Play(); Debug.Log("btn3"); break; default: break; } } } As you can see my code is the same as yours I just added the debug log so that I would get some feedback in my console.
@JoshuaMartinez-hu5bg6 жыл бұрын
Thanks for your quick reply. I included the Debug statements and, unsurprisingly, they do not print anything to the console. Let me get something straight: You run the program in editor, put up the image marker, and when the buttons appear on the game view, you try to click on them with the mouse. Right? Or am I doing it wrong when testing it in editor? This is the tutorial that taught me how to process input mouse clicks, so I learned the mouse position input processing from here (kzbin.info/www/bejne/qny7Zn6Ie8d6pJY) though the difference of course is that this is a regular Unity program but what I am trying to do is a Vuforia program where the buttons appear when image recognition succeeds and so the buttons move with the image, etc... I would appreciate your thought, so I can get this done. Thanks
@CubicBrain6 жыл бұрын
Hi Joshua, yes, I have the script attached to the image target, and then I just "scan" the image target with the webcam and click the cubes in the game view with my mouse. Did you check the naming of your buttons?
@JoshuaMartinez-hu5bg6 жыл бұрын
Yes, I did. It's strange... Do you think "distance" from the webcam might be an issue? I tried it, to no avail. Is there a script somewhere that deals with distance from the image target to the webcam or device cam?
@mumudo34455 жыл бұрын
In theory, this should work with ARFoundation? Can anyone answer this.
@CubicBrain5 жыл бұрын
Hi, Well i should, however I haven't tested it. but the setup is similar.
@yansen12076 жыл бұрын
Hi does this work with virtual button?
@CubicBrain6 жыл бұрын
Hi, Yes, but as far as I know virtual buttons are not on screen clickable.
@AkshayBharwani6 жыл бұрын
I made a similar app, and added normal UI buttons which were clickable through the screen. Without Ray Casting. Just normal button. Have you tried that?
@pimrumpanoithammaraj63736 жыл бұрын
Hi I built it in IOS and it didn't work. Is it has to build only in Andriod phone?
@CubicBrain6 жыл бұрын
Hi Pimrumpa. I havn't tried building it to a apple device (as I do not own one). however it should also work on iOS.
@pimrumpanoithammaraj63736 жыл бұрын
Everything was appeared except sound :( Thank you for this dope tutorial tho!
@rosannarifino93373 жыл бұрын
Why, when I press the cube in play with the mouse, nothing happens? Can someone help me, please?
@CubicBrain3 жыл бұрын
Hi Rosanna, this won't work with mouse click, as it's only registering touch input. You could look at this unity doc for the mouse version: docs.unity3d.com/ScriptReference/Input-mousePosition.html
@rosannarifino93373 жыл бұрын
@@CubicBrain thank you so much and really great video 👍
@judge555556 жыл бұрын
Is it possible to use this to trigger animations?
@CubicBrain6 жыл бұрын
Hi Juan, Sure. you just have to play the specific animation in the animator. One of my other videos might help you: kzbin.info/www/bejne/hGiWdoqnqrl6ibs
@judge555556 жыл бұрын
CubicBrain thanks. I'll check that out. It's hard being new at being a developer that does not know code hahaha.
@CubicBrain6 жыл бұрын
sure. let me know if you run into trouble.
@judge555556 жыл бұрын
CubicBrain thank you very much!
@judge555556 жыл бұрын
do you have discord by any chance? or better a server in discord?
@lilicafaray84233 жыл бұрын
i'm having some hiccups T.T mine won't work, but I use a custom made 3D character and I don't know what to do
@CubicBrain3 жыл бұрын
You might need to add a collider to your model, and match the name og that model to the button name. If that does not work, you can create a cube and make your model a child of that cube. Then just remove the mesh rendere for the cube.
@lilicafaray84233 жыл бұрын
@@CubicBrain thank you. The collider worked
@SkullKrusher0172 жыл бұрын
does it work on iOS
@CubicBrain2 жыл бұрын
yes it should work.
@HanaShoib6 жыл бұрын
Hi, what if you want to do something other than play a bit of audio, what if I want some text to pop up for example?
@CubicBrain6 жыл бұрын
Hi Hana. Then you just have to add the specific call in the switch case. like ex. if you have an animation component, then you could start an animation by writing: anim.Play("spinAnimation").
@HanaShoib6 жыл бұрын
CubicBrain ahh and I'll have to create that animation externally? As in - within the unity environment?
@CubicBrain6 жыл бұрын
you can create some simple animations within Unity, but thats primarily position, scale and rotation. external animation applications will be able to do a lot more. regarding loading and triggering the animation, you can watch my Unity trigger animations video (the one with FBX uses externally created animations)
@HanaShoib6 жыл бұрын
I see. do you have any tutorial on something like that that I can refer to? : )
@CubicBrain6 жыл бұрын
not regarding creating animations in external programs, but the setup and triggering, you can refer to this tutorial: kzbin.info/www/bejne/hGiWdoqnqrl6ibs
@miguelmascarenhas6135 жыл бұрын
0 dislikes damn that s rare
@fahadrahman44285 жыл бұрын
How can an animate an object using these buttons? Here is the code what I've been doing so far. using System.Collections; using System.Collections.Generic; using UnityEngine.SceneManagement; using UnityEngine; public class soundcon : MonoBehaviour { public AudioClip[] aClips; public AudioSource myAudioSource; string btnName; public Animator anim; // Use this for initialization void Start () { myAudioSource = GetComponent (); anim = GetComponent (); } // 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); RaycastHit Hit; if (Physics.Raycast (ray, out Hit)) { btnName = Hit.transform.name; switch (btnName) { case "myButton1": anim.Play ("point"); myAudioSource.clip = aClips [0]; myAudioSource.Play (); break; case "myButton2": SceneManager.LoadScene (0); break; default: break; } } } } } Thanks in advance :)