- Home /
Aim at touch (2D).
What im trying to achieve is a script that aims the gameobject it's attached to towards touch position. I have no problem doing this for standalone with the "lookAt" thingy, And i've tried to convert a touch posision to something that works with the lookAt with no succes.
I also tried using a mesh cursor where the gameobject is aimed at the cursor instead of touch, But this brought up some issues since it has to work over a network view.
What i tried was:
using UnityEngine;
using System.Collections;
public class Controller : MonoBehaviour {
void Update ()
{
int fingerCount = 0;
foreach (Touch touch in Input.touches) {
if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
fingerCount++;
if (fingerCount > 0){
transform.LookAt(Input.GetTouch(0).position);
}
}
}
}
But the position of the touch seems to be a little offset. (maybe due to the perspective and the depth position of the touch.)
Answer by ava4414 · Mar 29, 2014 at 06:02 PM
your problem could be this line:
transform.LookAt(Input.GetTouch(0).position);
you can solve it with:
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(touch.position);
Vector2 touchPos = Vector2(worldPoint .x, worldPoint .y);
transform.LookAt(touchPos);
this code will convert the screen touch position to the world position that its the one you need so it will solve the problem
it gives me this error: (17,52): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected
Do i have to create variables for X and Y?
Never$$anonymous$$d. The vector 2 on your second line should be "new Vector2"
I just tested it on a mobile device and there is something wrong with it. The object snaps into a certain rotation once you touch the screen and wont move again.
It appears that the Y axis of the object is either ai$$anonymous$$g towards the camera or somewhere very far from it.
EDIT: I forgot to change my camera to ortographic. It works fine now.
Answer by TinyRodent · Apr 09, 2019 at 06:04 AM
super late but im trying to achieve a similar goal to you and have a gameobject look at the users touch location. I cannot find any sources that have really helped but this has been the closest. The issue is though that when i do click on the screen the object looks the opposite way and even changes dimensions depending on the location of the click??!! I have followed your code to the T and cannot replicate the same results.
Hoping someone can help me out here.,super late but im trying to achieve a similar goal to you and have a gameobject look at the users touch location. I cannot find any sources that have really helped but this has been the closest. The issue is though that when i do click on the screen the object looks the opposite way and even changes dimensions depending on the location of the click??!! I have followed your code to the T and cannot replicate the same results.
Your answer
Follow this Question
Related Questions
Problem with gun rotation after flipping character (2D) 1 Answer
Shooting around defense position without contact 1 Answer
Touch position is off screen. 1 Answer
weapon aim when shooting 0 Answers
Drag object by Touch quickly 1 Answer