• 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
Question by London243 · Jun 19, 2014 at 06:32 PM · raycasttouchlinerendererfireaim

Aiming to an object (iphone game)

Hello,

I am quite stuck in this matter.

I am trying to fire a ray from a cube to a game object in an Iphone via touch in order to allow the player to aim at an object.

I try different solution but I learn that draw.line works online with gizmos, and GL do not work in iOS platforms

So I did try this solution after search in all documents in Unity

However I did maker it work in my PC but when I put the touch part it all stuck.

Please any advice will be great since is one week I am stuck in this

This is my script:

 public class Linerender : MonoBehaviour
 {
 
     private LineRenderer lineRenderer;
     private float counter;
     private float dist;
 
     public Transform origin;
     public Transform destination;
 
     public float lineDrawSpeed = 6f;
  
 
     void Start () 
     {
         lineRenderer = GetComponent<LineRenderer> ();
         lineRenderer.SetPosition (0, origin.position);
         lineRenderer.SetWidth (.45f, .45f);
     }
     
     void Update ()
     {
         
                 
                 //loop through all the touches on the screen 
                 
                 for(int i = 0 ; i < Input.touchCount; i++)
                 {
                     if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Stationary)
                     {
 
                         dist = Vector3.Distance(origin.position, destination.position);
 
                         if(counter < dist)
                         {
                             counter += .1f / lineDrawSpeed;
 
                             float x = Mathf.Lerp(0, dist, counter);
                             Vector3 pointA = origin.position;
                             Vector3 pointB = Input.GetTouch(0).position;
 
                         //get the unit vector in the desired direction, multiplay by the desired leght and add the staring point 
 
                             Vector3 pointAlongLine = x * Vector3.Normalize(pointB - pointA) + pointA;
 
                             lineRenderer.SetPosition(1, pointAlongLine);
 
                     }




Any help please. I would describe myself as desperate. I did try several way but when I put the touch part it simply doesn't work

Thanks

CL

Comment

People who like this

0 Show 1
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 Adam-Buckner ♦♦ · Jun 24, 2014 at 09:59 AM 0
Share

Fixed code tags

1 Reply

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by robertbu · Jun 19, 2014 at 06:36 PM

Touch works in Screen coordinates. LineRenderer works is world coordiantes. You have to convert between the two. How you do that conversion will depend on the kind of camera (Orthographic vs Perspective), and the rotation of the camera. If the camera has rotation (0,0,0), and the object plane is 10 units in front of the camera, you would do:

 Vector3 pointB = Input.GetTouch(0).position;
 pointB.z = 10;
 pointB = Camera.main.ScreenToWorldPoint(pointB);

Note your code is using Input.GetTouch(0). This means for all the touches, you are only processing the first touch. So either you should dispense with the for() loop and just process the first touch if touchCount > 0, or you would use Input.GetTouch(i). The latter would require a LineRenderer for each touch.

Comment

People who like this

0 Show 2 · Share
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 London243 · Jun 20, 2014 at 08:22 AM 0
Share

Hello Robertbu,

I try to implement as you said and still nothing happen.

Here is the code. I don't know if the calculation or something else but all that happens is that there is line out of my cube that doesn't move. I did included all the code this time.

 private LineRenderer lineRenderer;
 private float counter;
 private float dist;
 
 public Transform origin;
 public Transform destination;
 
 public float lineDrawSpeed = 6f;
 
 
 
 // Use this for initialization
 void Start () 
 {
     lineRenderer = GetComponent<LineRenderer> ();
     lineRenderer.SetPosition (0, origin.position);
     lineRenderer.SetWidth (.45f, .45f);
 }
 
 // Update is called once per frame
 void Update ()
 {
     {
         if (Input.touches.Length <= 0) 
         {
             //if there is no touches on the screen  the this code
             
             return;
         } 
         
         else // if there is a touch
             
         {
             
             //loop through all the touches on the screen 
             
             for(int i = 0 ; i < Input.touchCount; i++)
             {
                 if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)
                 {
                     
                     dist = Vector3.Distance(origin.position, destination.position);
                     
                     if(counter < dist)
                     {
                         counter += 0.1f / lineDrawSpeed;
                         
                         float x = Mathf.Lerp(0, dist, counter);
                         Vector3 pointA = origin.position;
                         Vector3 pointB = Input.GetTouch(0).position;
                         pointB.z = 50;
                         pointB = Camera.main.ScreenToWorldPoint(pointB);
                         
                         //get the unit vector in the desired direction, multiplay by the desired leght and add the staring point 
                         
                         Vector3 pointAlongLine = x * Vector3.Normalize(pointB - pointA) + pointA;
                         
                         lineRenderer.SetPosition(1, pointAlongLine);
                         
                         
                     }
                     if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Stationary)
                         
                         dist = Vector3.Distance(origin.position, destination.position);
                     
                     if(counter < dist)
                     {
                         counter += 0.1f / lineDrawSpeed;
                         
                         float x = Mathf.Lerp(0, dist, counter);
                         Vector3 pointA = origin.position;
                         Vector3 pointB = Input.GetTouch(0).position;
                         pointB.z = 50;
                         pointB = Camera.main.ScreenToWorldPoint(pointB);
                         
                         //get the unit vector in the desired direction, multiplay by the desired leght and add the staring point 
                         
                         Vector3 pointAlongLine = x * Vector3.Normalize(pointB - pointA) + pointA;
                         
                         lineRenderer.SetPosition(1, pointAlongLine);
                     }


avatar image Adam-Buckner ♦♦ · Jun 24, 2014 at 10:09 AM 0
Share

There seems to be something critically fatal about the code you have posted here.

If I look at the number of braces/brackets... some of the code is nested incorrectly to work.

The line:

if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Stationary)

... is nested inside:

if (Input.touchCount > 0 && Input.GetTouch (0).phase == TouchPhase.Began)

This means you cannot ever get the code within the one checking for TouchPhase.Stationary as it's withing the code checking for TouchPhase.Began.

It's hard to work through the logic of the line renderer, when the logic of the basic script is flawed.

Can we get a working touch script before we work on the line renderer?

Or change platforms and get this working 100% on a standard platform (Stand-alone, Web) and then get the touch details working after we know our line renderer code is working?

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Touch and aim with a raycast line 2 Answers

How can I make raycast touch responsive with 2d world objects? Raycast misses the collider when finger is moved at faster rate. 0 Answers

How can I can I cast a ray from a gameobject? 1 Answer

throw an object by swiping on it 0 Answers

How would I set the end of my line renderer to the first collision point with a raycast? 0 Answers


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