- Home /
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
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.
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 = $$anonymous$$athf.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 = $$anonymous$$athf.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);
}
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
![](https://koobas.hobune.stream/wayback/20220613151946im_/https://answers.unity.com/themes/thub/images/avi.jpg)