x


Shooting with camera on rails

Hi all, im quite new to Unity and am working on a simple shooter on rails, however I am having trouble getting the projectiles when you shott to head out in the right direction.

I have made a gun object that is attatched to the camera, I need the gun object to shoot out wherever the player clicks, im not sure if this is the best way to do it or if I should maybe use raycasting instead?.

Here is the code I have so far, any help would be greatly appreciated.

#pragma strict
var projectile : Rigidbody;
var speed = 20;
var mousex = 0;
var mousey = 0;
var gun : GameObject;

function Start () {

}

function Update () {
  mousex = Input.mousePosition.x/2;
  mousey = Input.mousePosition.y/2;
  gun.transform.LookAt(Vector3(mousex,mousey,0));

  if (Input.GetButtonDown("Fire1"))
  {
    var instantiatedProjectile : Rigidbody = Instantiate( projectile, transform.position, transform.rotation);
    instantiatedProjectile.velocity = transform.TransformDirection( Vector3(0,0, speed));
    Physics.IgnoreCollision( instantiatedProjectile. collider,transform.root.collider);
  }
}
more ▼

asked May 26 '12 at 02:57 PM

thefol gravatar image

thefol
-3 1 2 3

(comments are locked)
10|3000 characters needed characters left

4 answers: sort newest

Your first problem is in the fact that you are just using the mouse coordinates - they aren't in work space so that will give you a problem right there :)

You want to use:

 var ray : Ray = Camera.mainCamera.ScreenPointToRay(Input.mousePosition);

This gives you a ray, now you need to decide how far from the camera the target is and do this

 gun.transform.LookAt(ray.GetPoint(distanceFromTheCamera))
more ▼

answered May 26 '12 at 04:09 PM

whydoidoit gravatar image

whydoidoit
33k 11 23 98

That worked great thanks.

May 26 '12 at 08:39 PM thefol
(comments are locked)
10|3000 characters needed characters left

Hi, its a Rocket, so I do want it to be visible, ive no idea how to raycast either.

Thanks for the reply.

more ▼

answered May 26 '12 at 08:01 PM

thefol gravatar image

thefol
-3 1 2 3

(comments are locked)
10|3000 characters needed characters left

Hi Mike, That works great, thank you.

more ▼

answered May 26 '12 at 08:00 PM

thefol gravatar image

thefol
-3 1 2 3

(comments are locked)
10|3000 characters needed characters left

How fast is your projectile ? Bullet, arrow, bubble ? If the projectile is too fast to even be seen, don't bother with a GameObject at all, use a raycast and something for the impact. For an arrow-like speed, it becomes relevant to have a body.

more ▼

answered May 26 '12 at 03:15 PM

Berenger gravatar image

Berenger
11k 12 19 53

(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:

x982
x329
x129
x2

asked: May 26 '12 at 02:57 PM

Seen: 544 times

Last Updated: May 26 '12 at 08:39 PM