- Home /
Rocket launcher script help
Hi, I have a fps and I have an enemy robot attack any object that has the tag player. (other objects have the tag player as well) I have a rocket launcher object and script that are children of the robot. Here is the first script:
RocketLauncher.js
#pragma strict
var projectile : Rigidbody;
var initialSpeed = 20.0;
var reloadTime = 0.5;
var ammoCount = 20;
private var lastShot = -10.0;
private var cc : CharacterController;
var player : Transform;
function Start() {
//var go = GameObject.Find("First Person Controller");
var go = player;
go = GameObject.FindWithTag("Player").transform;
player = go.transform;
cc = go.GetComponent(CharacterController);
}
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);
// Time of flight to reach player
var t = (player.position - transform.position).magnitude / initialSpeed;
// Project future position
var futurePos = player.position + cc.velocity * t;
// Aim and fire at that future position
var aim = (futurePos - transform.position).normalized;
instantiatedProjectile.transform.rotation = Quaternion.LookRotation(aim);
instantiatedProjectile.velocity = aim * initialSpeed;
// Ignore collisions between the missile and the character controller
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
ammoCount--;
}
}
It tracks the target the points the rocket the right way then fires. It works fine until the robot kills target which is the same object that is assigned to the player variable. Then the rockets don't go anywhere and i get an error. I want the robot to send its target value to the launchers target value. Can someone please post a revision of the code? Thank you in advance.
Answer by robertbu · Aug 18, 2013 at 01:29 AM
Assuming you are targeting any object that has the tag 'Player', you just need to find the 'player' transform each time you need to fire:
#pragma strict
var projectile : Rigidbody;
var initialSpeed = 20.0;
var reloadTime = 0.5;
var ammoCount = 20;
private var lastShot = -10.0;
private var cc : CharacterController;
var player : Transform;
function Start() {
cc = go.GetComponent(CharacterController);
}
function Fire () {
// Did the time exceed the reload time?
if (Time.time > reloadTime + lastShot && ammoCount > 0) {
var go = GameObject.FindWithTag("Player")
if (go == null) {
return;
}
player = go.transform;
// create a new projectile, use the same position and rotation as the Launcher.
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
// Time of flight to reach player
var t = (player.position - transform.position).magnitude / initialSpeed;
// Project future position
var futurePos = player.position + cc.velocity * t;
// Aim and fire at that future position
var aim = (futurePos - transform.position).normalized;
instantiatedProjectile.transform.rotation = Quaternion.LookRotation(aim);
instantiatedProjectile.velocity = aim * initialSpeed;
// Ignore collisions between the missile and the character controller
Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
lastShot = Time.time;
ammoCount--;
}
}
This doesn't work right. It keeps on giving me an error saying that the target was destroyed.
I made a change that will fix the condition where there are no Players to be found. This may be your problem
Note I cannot easily run and test this code, so I have to depend you to tell me if something does not work right. And I need complete information including the exact error in the condole and a full description of the behavior.