Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by javoastudillo · Mar 21, 2017 at 02:17 AM · androidlinerenderertouchscreenraycasthit2d

Failing every method for touch point in Android screen

I can't see why on earth this script runs perfectly OK in the Unity Editor but fails in Android tablet. I suspect that the problem is in getting the mouse events because it is not going inside the if block no matter the method I use as condition (see the +++comment+++ in code), probably due to the difference between mouse and finger positioning. Yet I can't figure out the solution. I coded a sound to be played in the if block, so that I know where exactly the problem is.

This is a 2D game where I must draw a line over a predefined path (collider 'area_0' or 'area_1') without getting out of it. So I'm raycasting finger position against the collider.

In Unity Editor works like a charm, once I push it to the tablet it just doesn't.

I adapted the line drawing script some guy published in YouTube tutorial.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Dibujar : MonoBehaviour {
     private Color color;
 
     void Start() {
         color = new Color32(0xFF, 0xED, 0x00, 0xFF);
     }
 
     // Update is called once per frame
     void Update() {
         if (Input.GetMouseButtonDown(0)) {
         //if (Input.GetKeyDown(KeyCode.Mouse0))
             // +++++++ DOES NOT GET IN HERE +++++++
             StartCoroutine(dibujar());
         }
     }
 
     IEnumerator dibujar() {
         LineRenderer linea = new GameObject().AddComponent<LineRenderer>();
         List<Vector3> posiciones = new List<Vector3>();
         bool borrar = false;
 
         linea.startWidth = 0.2f;
         linea.endWidth = 0.2f;
         linea.material = new Material(Shader.Find("Unlit/Color"));
         linea.material.color = color;
         linea.transform.SetParent(transform);
         linea.numCapVertices = 1;
         linea.numCornerVertices = 1;
 
         while (Input.GetMouseButton(0)) {
         //while (Input.GetKey(KeyCode.Mouse0)) {
         //while (Input.touchCount > 0 || Input.touches[0].phase == TouchPhase.Began) {

             posiciones.Add(Camera.main.ScreenToWorldPoint(Input.mousePosition) + Vector3.forward);
             linea.numPositions = posiciones.Count;
             linea.SetPositions(posiciones.ToArray());
 
             Vecto3 rayo = Camera.main.ScreenToWorldPoint(Input.mousePosition);
             Debug.DrawRay(rayo, Vector3.forward * 20);
             RaycastHit2D hit = Physics2D.Raycast(rayo, Vector3.forward, Mathf.Infinity);
             
             if (hit.collider == null) {
                 // if no hit
                 }
                 borrar = true;
                 break; // force while loop to stop drawing
             }
 
             if (hit.collider.gameObject.name == "area_0") {
                 // if hit area_0
             }
 
             if (hit.collider.gameObject.name == "area_1") {
                 // if hit area_1
             }
 
             yield return new WaitForSeconds(0);
         }
 
         if (borrar) {
             // erase line
         }
     }
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image javoastudillo · Mar 20, 2017 at 10:59 PM 0
Share

BTW, I have changed Vector3.forward for Vector2.zero with no different result. The problem doesn't seem to be in raycasting. Or does it?

avatar image javoastudillo · Mar 21, 2017 at 01:42 AM 0
Share

$$anonymous$$aybe it gets confused because of coroutine?

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Smooth draw curve line 0 Answers

How to make an android joystick like Arcane Legends 0 Answers

How can i use touch input on android 2d game in unity? 0 Answers

Unity Android Custom button touch screen going to one level instead of the level picked 2 Answers

Drag a ball to add force to a ball by touch screen 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges