Up next

Detecting TOUCH Inputs - Unity Mobile Game Dev Tutorials

0 Views· 03/09/24
wisdom
wisdom
3 Subscribers
3

ACCESS the FULL COURSE here: https://academy.zenva.com/product/mobile-gamedev-mini-degree/?utm_campaign=youtube_description&utm_medium=youtube&utm_content=youtube_2019_detectingtouchinputsunity

TRANSCRIPT

Hey everyone, in this lesson, we are going to be setting it up so we can actually touch stuff on the screen. We'll be detecting touch inputs, and then we'll be shooting a ray cast towards what we touched and interacting with that object in some way. What we're gonna be doing with the interaction is actually changing the color of whatever we touched to a random new color. So what we're gonna do now is go to our Scripts folder, I'm gonna right click here, I'm gonna create a new C# script called ColorChanger. And what I'm gonna do is also just attach this to our main camera here And then we can open up the script inside of Visual Studio. Okay, so to begin, we are going to actually just have one function, and that one function is going to be the update function, which gets called every single frame of the game. And this is actually the only function that we're gonna be needing and using in this script. So, inside of the update function, what do we wanna do? So the first thing I'm going to do in the update function is check are we touching the screen? So we can go if input to access the actual input class of Unity which has many things such as keyboard inputs, mouse inputs, and of course touch inputs. So it will go if input .touchCount if this touchCount is greater than zero, so if there are more than zero touches on the screen and the input.touches, now touch input.touches is a array of all the different touches that are currently on the screen, so to access the first or if there is only one touch on the screen, we can just access the zero element, the first element of the array. If that touch.phase, now there's also a phase for a touch. And there are multiple different phases such as began, moved, stationary, released. So if this touch phase equals equals, TouchPhase.Began, so, if there is more than one touch on the screen, and that touch has just begun, if this is the frame that this touch has just touched the screen, then what we're going to do is create a ray cast from where we touched it on the screen first of all. So it will create a new ray here, and it's gonna be equal to our Camera.main.ScreenPointToRay. And the point on the screen we want to create a ray cast from is going to be the Input.touches, the first touch .position. So this is gonna create a ray cast on the screen from where we touched, and it's gonna be shooting at whatever we are pointing at. Since we are gonna be hitting an object and we need to know information about that object, we also gonna create a RaycastHit object here, which is gonna store all the info of whatever we hit. And finally down here we can shoot the ray cast by going if Physics.Raycast we can enter in our ray. And then we wanna send this out to the hit of pretty much whatever we hit, we wanna send it to hit. And so if we shoot that Raycast, what we wanna first check of all is did we hit something? So, if hit.collider doesn't equal null, if we did hit something, then what we want to do is change this object mesh renderer color. So I'm gonna create a color variable here, I'm just gonna call this our newColor. And this is just gonna be equal to a new Color and we'll just give it a random red, green and blue values. So I'll just give it a Random.Range between 0.0 and 1.0 because this Color class here, it takes in its RGB and A values not as zero to 255 as it would be in the editor, it's actually between zero and one. So we'll do that. Then actually, I'll just copy this Random.Range here and paste this for the green and blue values.

Show more

 0 Comments sort   Sort By


Facebook Comments

Up next