x


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?

more ▼

asked May 10 '12 at 02:35 PM

oziriz gravatar image

oziriz
0 1 1 1

Just in general, you should cache the player gameObject, instead 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.

May 10 '12 at 02:37 PM syclamoth

I started unity very recently...tanks anyway ;)

May 10 '12 at 02:54 PM oziriz
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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?

more ▼

answered May 10 '12 at 03:08 PM

Swift_On_Fire gravatar image

Swift_On_Fire
101 3 6

May 10 '12 at 03:13 PM oziriz
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x577
x329
x151
x39

asked: May 10 '12 at 02:35 PM

Seen: 631 times

Last Updated: May 10 '12 at 03:13 PM