- Home /
How to aim at the point you're touching?
Hi, I' trying to develop a Sidescroller-Shooter Game. I've got my Character equipped with a pistol. The pistol is a child of an Empty Gameobject, that is in the middle of the Character, so that the Pistol can Rotate around it.
Now i want my gun to aim at the point, where I'm touching the screen. But when I put the Touch.position data in to the new Vector3, where I want the Gun to aim at. It is somewhere in outer Space, like 512 on x.
function Guncontroll() {
if(Input.touchCount > 0){
var i : Vector2 = Input.GetTouch(0).position;
aimpoint.position = (Vector3 (i.x, i.y, 0);
gun.LookAt(aimpoint);
}
}
Hope someone could help me :)
Answer by Scribe · Jun 10, 2013 at 06:33 PM
Input.GetTouch(0).position is the position on screen in pixels.
Use Camera.ScreenToWorldPoint to convert it into 'world' space
Scribe
If I write it like this:
function Guncontroll() { if(Input.touchCount > 0){ var i : Vector2 = Input.GetTouch(0).position; aimpoint.position = playercamera.ScreenToWorldPoint(Vector3 (i.x, i.y, 0)); gun.LookAt(aimpoint); } }
The point always keeps on the position of my camera.
(playercamera is assigned)
Change the 0 to the distance between you camera and your character and see if that works.
Do you maybe also know, how to exclude the joystick fields from that, but should be able with mul$$anonymous$$ch?
Glad you got it working,
By Joystick do you have a joystick device plugged in? Or is it some GUI joystick image?
$$anonymous$$ight be best to ask it in a new question!
Your answer
Follow this Question
Related Questions
Unity Car Accelerometer 0 Answers
Move to touch position 1 Answer
Tracking Touch Points on Screen for Mobile Devices 1 Answer
How to get TouchPosition ?? 1 Answer
Touches aren't working on Android 1 Answer