- Home /
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--;
}
}
Format your code by selecting it and hitting the Code Format button.
Thanks. This is my first post and I couldnt figure that out.
Answer by Mike 3 · Jul 29, 2010 at 07:47 PM
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;
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.
It may be better to explain how you're doing the crosshairs then, I assumed it was where the mouse was.
Thanks for the help $$anonymous$$ike. It sent me in the right direction of figuring out the answer.
Answer by behnker · Aug 03, 2010 at 07:15 PM
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--;
}
}
Anyway to change this so the projectile doesn't hit instantly?
Your answer
Follow this Question
Related Questions
Projectile Hit Detection with Bullet Drop 1 Answer
Random Raycast inside crosshairs 1 Answer
Problem in Shooting Accuracy 0 Answers
How to stop enemies from shooting each other 1 Answer
Aiming to the crosshair 1 Answer