- Home /
Shooting with touch
Hi! I am developing a 3d shooter and i was trying to shoot my enemy using rigidbody and then addForce. It all works great when i use the mouse but i wanted to use small taps to shoot bullets. I am using the following code to catch the touch input:
My goal is to shoot the area that i am touching (without moving the cam)
// touch,tap code
float point;
foreach(Touch touch in Input.touches){
player = new Vector3(GameObject.Find("Player").transform.position.x,GameObject.Find("Player").transform.position.y,GameObject.Find("Player").transform.position.z);
Plane playerPlane = new Plane(player,Camera.main.transform.forward);//VER ESTA LINHA
Ray camRay = Camera.main.ScreenPointToRay(touch.position);
playerPlane.Raycast(camRay,out point);
Vector3 position = camRay.GetPoint(point);
Ray shotRay = new Ray(GameObject.Find("Player").transform.position, (position - GameObject.Find("Player").transform.position));
Instantiate();//function that does the shooting
canShoot = false;
}
//shooting code
Transform crate = Instantiate(cratePrefab, transform.position, Quaternion.identity) as Transform;
Physics.IgnoreCollision(crate.collider, collider);
crate.rigidbody.AddForce(transform.forward * 5000);
How can i use the "ray" with the function i have to shoot bullets?
Just in general, you should cache the player gameObject, ins$$anonymous$$d of using GameObject.Find(name). Totally unrelated to the question, but it really bugs me every time I see someone using GameObject.Find for anything.
Answer by Swift_On_Fire · May 10, 2012 at 03:08 PM
I agree with syclamoth about caching the player object. That aside...
I don't see why you are using a Plane here. This looks more complicated than it has to be. Is this a 3rd person view or a 1st person view?
1st person! I followed a lead by another user -> http://unity3d.qatohost.com/questions/222121/raycast-from-player-to-touch-point.html
Your answer
Follow this Question
Related Questions
Tap Where To Shoot 2D 1 Answer
Raycast hitting below mouse position :( 0 Answers
Ray from a cube to a touch point to help aiming 0 Answers
Taps are not detected on mobile 1 Answer
TouchPhase.Ended does not work if ray is not hitting object. 1 Answer