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--;
}
}
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--;
}
}