- Home /
Problem getting touch input
void FixedUpdate ()
{
if (Input.touchCount == 1)
{
Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
Collider[] colliders = Physics.OverlapSphere(hit.point, radius);
foreach(Collider c in colliders)
{
if (c.GetComponent<Rigidbody>() == null)
continue;
c.attachedRigidbody.AddExplosionForce(force, hit.point, radius,
rise, ForceMode.Impulse);
Hey everyone. I'm trying to create a small mobile game where the user touches the screen and creates explosions to destroy buildings and such. However, I can't seem to get the code for the input right. I've tried to find the information I need in the Unity Documentation, but came up short. If anyone can show me my error or point to a place that can, I'd greatly appreciate it. Thanks for your time!
Just an update. Honestly this is the first time I've tried to do anything with mobile scripting so I'm not even sure I'm in the ballpark. But I changed a few things around just trying to make it work and this is where I am now. Am I at least closer?
void Update () { if (Input.touchCount >= 1) { foreach(Touch touch in Input.touches) { Ray ray = Camera.main.ScreenPointToRay(touch.position); RaycastHit hit;
if (Physics.Raycast(ray, out hit, 100))
{
Collider[] colliders = Physics.OverlapSphere(hit.point, radius);
foreach(Collider c in colliders)
{
if (c.GetComponent<Rigidbody>() == null)
continue;
c.attachedRigidbody.AddExplosionForce(force, hit.point, radius,
rise, Force$$anonymous$$ode.Impulse);
I don't have a quote for this, but I believe Input.Get$$anonymous$$ouseButtonDown(0) returns true for taps on a screen for mobile.
Increase the distance you are drawing your ray, change that 100 to 10000 just to make sure it is going far enough. Secondly, I assume you have an object or plane on the screen, you need something for the ray to hit - also make sure the object has a box or mesh collider.
I'd like to thank both of you guys for your help. I tried Get$$anonymous$$ouseButtonDown(0) and that seems to have done it. Just to be sure, I went ahead and deployed the test level to my device and sure enough, it accepted the tap input. Again, thanks guys!