x


Air Strike shoot aiming at cross hairs in middle of the screen

I am looking to create an airstrike as a third weapon of my game. I can get it to fire a rocket and it fires in the direction that im facing but the issue that im running into is having it hit where my cross hairs are facing. It would also be a possibility of creating new cross hairs while in the airstrike mode that are on the ground in front of you. Here is my current code. Any thoughts?

//code current

var projectile : Rigidbody;

var initialSpeed = 20.0;

var reloadTime = 0.5;

var ammoCount = 20;

private var lastShot = -10.0;

function Fire () {

    // Did the time exceed the reload time?
    if (Time.time > reloadTime + lastShot && ammoCount > 0) {
        // create a new projectile, use the same position and rotation as the Launcher.
        var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);

        // Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
        instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));

        // Ignore collisions between the missile and the character controller
        Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);

        lastShot = Time.time;
        ammoCount--;
    }
}
more ▼

asked Jul 29 '10 at 06:22 PM

behnker gravatar image

behnker
11 4 4 9

Format your code by selecting it and hitting the Code Format button.

Jul 29 '10 at 06:47 PM qJake

Thanks. This is my first post and I couldnt figure that out.

Jul 29 '10 at 07:28 PM behnker
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

By the looks of it, just change:

instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));

to

instantiatedProjectile.velocity = Camera.main.ScreenPointToRay(Input.mousePosition).direction * initialSpeed;
more ▼

answered Jul 29 '10 at 07:47 PM

Mike 3 gravatar image

Mike 3
30.7k 10 67 255

It didnt work. I think its because there is not mousePosition. I trying to add this to the FPS tutorial for testing purposes. So you have your center crosshairs and no mouse. The airstrike spawns way above the character. It shoots in the direction of the cross hairs just not in the center of the screen that I am looking at. I think I have to switch this to some form of RayCast just not certain on how that works fully.

Jul 29 '10 at 08:04 PM behnker

It may be better to explain how you're doing the crosshairs then, I assumed it was where the mouse was.

Jul 29 '10 at 08:28 PM Mike 3

Thanks for the help Mike. It sent me in the right direction of figuring out the answer.

Aug 03 '10 at 07:16 PM behnker
(comments are locked)
10|3000 characters needed characters left

Ok I figured it out. Below is the code that works for me. If anyone is going to set this up in their game I would say go a step further and add a cross hair that runs accross the ground and put the game object with this script attached to the ground cross hair that way when ever you do the air strike it shoots down onto the object.

var projectile : Rigidbody;
var initialSpeed = 20.0;
var reloadTime = 0.5;
var ammoCount = 20;
private var lastShot = -10.0;


function Fire () {
    // Did the time exceed the reload time?
    if (Time.time > reloadTime + lastShot && ammoCount > 0) {
        // create a new projectile, use the same position and rotation as the Launcher.
        var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);

        // Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
        //instantiatedProjectile.velocity = Camera.main.ScreenPointToRay(Input.mousePosition).direction * initialSpeed;;

        //Sets up the RayCast to the direction of the cross hairs
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        var hit : RaycastHit;
        Physics.Raycast(ray, hit);

        //gives position of the RayCast
        instantiatedProjectile.MovePosition(hit.point);

        //gives the initial forward velocity
        instantiatedProjectile.velocity = ray.direction * initialSpeed;

        // Ignore collisions between the missile and the character controller
        Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);

        lastShot = Time.time;
        ammoCount--;
    }
}
more ▼

answered Aug 03 '10 at 07:15 PM

behnker gravatar image

behnker
11 4 4 9

Anyway to change this so the projectile doesn't hit instantly?

Aug 03 '10 at 09:16 PM Fishman92

@Fishman92 -maybe adjust the initialspeed variable

Mar 27 '11 at 08:21 PM Ullukai
(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:

x1580
x330
x285
x261
x111

asked: Jul 29 '10 at 06:22 PM

Seen: 1833 times

Last Updated: Jul 29 '10 at 06:46 PM