- Home /
How to make this enemy script shoot upwards?
Hey,
I'm new to unity and I have this script for one of my enemies. The only problem is that I've been trying to make them shoot upwards when I jump in the air. They only seem to shoot straight and not on the Y axis. I've been looking for a solution on the internet for a couple of days now, but without any luck so I decided to try here.
Thank you for your time!
private var target : Transform;
var rotateSpeed = 20;
var moveSpeed = 5;
var hitPoints = 10;
var bulletDEAD : Transform;
var explosionDEAD : Transform;
private var dead : boolean = false;
private var currentHitPoints = hitPoints;
var liftForce = 50;
var projectile : Rigidbody;
var Points1 : GameObject;
var Points2 : GameObject;
var ScoreAmount = 10;
var force = 12.0;
var damage = 5.0;
var bulletsPerClip = 60;
static var clips = 20;
var reloadTime = 0.5;
var hitParticles : ParticleEmitter;
var muzzleFlash : Renderer;
public var triggerTime = 0.05f;
private var bulletsLeft : int = 0;
private var nextFireTime = 0.0;
private var m_LastFrameShot = -1;
var bulletVelocity = 40;
function Start () {
// get the ParticleEmitter component from the object
hitParticles = GetComponentInChildren(ParticleEmitter);
// emit particles only when we hit something.
if (hitParticles)
hitParticles.emit = false;
bulletsLeft = bulletsPerClip;
// search for the player tagger "player"
if (target == null && GameObject.FindWithTag("Player"))
target = GameObject.FindWithTag("Player").transform;
currentHitPoints = hitPoints;
}
// function to destroy the object if it gets out of bounds
function ColliderBoundDamage (damage : float) {
var spawner : SpawnManager = GetComponent(SpawnManager);
if (currentHitPoints <= 0)
return;
currentHitPoints -= damage;
if (!dead && currentHitPoints <= 0) {
dead = true; // changes the "dead" boolean option to true
Destroy(gameObject); // destroy the gameObject
spawner.amountEnemies -= 1; // inform the spawn manager about the object's death
}
}
// function for getting killed by a bullet
function ApplyBulletDamage (damage : float) {
var spawner : SpawnManager = GetComponent(SpawnManager);
if (currentHitPoints <= 0)
return;
currentHitPoints -= damage;
if (!dead && currentHitPoints <= 0) {
dead = true;
Destroy(gameObject);
Instantiate(Points1, transform.position, Quaternion.identity); // spawn a 3d text containing information about points
Instantiate(bulletDEAD, transform.position, transform.rotation); // spawn a dead replacement of the object
spawner.amountEnemies -= 1; // inform the spawn manager about the object's death
GUIController.MyScoreCounter += ScoreAmount; // Add scores!
}
}
//function for getting killed by an explosion
function ApplyExplosionDamage (damage : float) {
var spawner : SpawnManager = GetComponent(SpawnManager);
if (currentHitPoints <= 0)
return;
currentHitPoints -= damage;
if (!dead && currentHitPoints <= 0) {
dead = true;
Destroy(gameObject);
Instantiate(Points2, transform.position, Quaternion.identity); // spawn a 3d text containing information about points
Instantiate(explosionDEAD, transform.position, transform.rotation); // spawn a dead replacement of the object
spawner.amountEnemies -= 1; // inform the spawn manager about the object's death
GUIController.MyScoreCounter += ScoreAmount; // Add scores!
}
}
function FixedUpdate () {
if (target == null)
return;
var direction = transform.TransformDirection(Vector3.forward);
var hit : RaycastHit;
// the climb object part
if (Physics.Raycast(transform.position, direction, hit, 3)) { // raycast 3 units ahead of the object
if (hit.transform.tag == "Climb" && !dead) { // if the raycast hits an object tagged "Climb"
transform.rigidbody.AddForce(Vector3.up *liftForce); // lift the object up until it overcomes the obstacle
}
}
// the rotate part
if (target && !dead) {
var rot = Quaternion.LookRotation(target.position - transform.position);
var rotationX = rot.eulerAngles.y;
var xQuaternion = Quaternion.AngleAxis(rotationX, Vector3(0, 1l, 0));
transform.localRotation = Quaternion.Slerp(transform.localRotation, xQuaternion, Time.deltaTime * rotateSpeed);
}
// the shoot part
// detect the distance between the player and the enemy
var dist = Vector3.Distance(target.position, transform.position);
if ( dist < Random.Range(20, 30) ) // if in range, shoot!
Fire();
else
// if not, keep walking!
if ( dist < Random.Range(50, 60) )
transform.rigidbody.MovePosition(transform.rigidbody.position + transform.TransformDirection(0, 0, moveSpeed) * Time.deltaTime);
}
function LateUpdate() {
if (muzzleFlash) {
if (m_LastFrameShot == Time.frameCount) {
muzzleFlash.enabled = true;
} else {
// Don't show muccle flash if we're not firing
muzzleFlash.enabled = false;
enabled = false;
// Play Gunfire sound
if (audio)
audio.Play();
audio.loop = false;
}
}
}
//the fire part
function Fire () {
var fireRate = Random.Range(0.1, 0.2); // give a small range of fireRate values to the enemies
// Keep firing until we used up the fire time
while( nextFireTime < Time.time && bulletsLeft != 0) {
FireOneShot();
nextFireTime += fireRate;
}
}
function FireOneShot () {
var direction = transform.TransformDirection(Random.Range(-0.05f, 0.05f) * triggerTime, Random.Range(-0.05f, 0.05f) * triggerTime, 1);
var hit : RaycastHit;
var clone : Rigidbody;
audio.Play(); // play the shoot sound
// instantiate a projectile
clone = Instantiate(projectile, transform.position, transform.rotation);
// and make it shoot forward by giving it a velocity
clone.velocity = transform.TransformDirection (Vector3.forward * bulletVelocity);
// ignore collision with the roof collider so that the enemy doesnt hurt itself from shooting
Physics.IgnoreCollision( clone.collider, transform.root.collider );
// raycasts forward
if (Physics.Raycast (transform.position, direction, hit)) {
// Apply a force to the rigidbody we hit
if (hit.rigidbody)
hit.rigidbody.AddForceAtPosition(force * direction, hit.point);
// spawn hit particle effects to the normals we hit
if (hitParticles) {
hitParticles.transform.position = hit.point;
hitParticles.transform.rotation = Quaternion.FromToRotation(Vector3.up, hit.normal);
hitParticles.Emit();
}
// Send a damage message to the hit object
hit.collider.SendMessageUpwards("ApplyPlayerDamage", damage, SendMessageOptions.DontRequireReceiver);
hit.collider.SendMessageUpwards("ApplyPropsDamage", damage, SendMessageOptions.DontRequireReceiver);
}
// get bullets left
bulletsLeft--;
// Register that we shot this frame,
// so that the LateUpdate function enabled the muzzleflash renderer for one frame
m_LastFrameShot = Time.frameCount;
enabled = true;
// Reload gun in reload Time
if (bulletsLeft == 0)
Reload();
return;
}
// a reload function to make sure the enemy isn't overpowered by giving it a brief reload time every
// time it shoots
function Reload (){
// wait for a specific amount of time
yield WaitForSeconds(reloadTime);
clips--;
bulletsLeft = bulletsPerClip;
}
Answer by bompi88 · Apr 27, 2012 at 01:24 PM
here is your problem i think:
// and make it shoot forward by giving it a velocity
clone.velocity = transform.TransformDirection (Vector3.forward * bulletVelocity);
you should do something like:
var ShootDir : Vector3;
if (!InJump) {
ShootDir = Vector3.forward;
} else {
// Change 0.3 to what fits you.
ShootDir = Vector3.forward + Vector3(0,0.3,0);
}
clone.velocity = transform.TransformDirection (ShootDir * bulletVelocity);
The InJump is a boolean you can change in your jump code. You would also have to update your raycast direction with the same value, i think. PS.: I`m usually coding in c#.
Your answer
Follow this Question
Related Questions
enemy shoot AI precision 1 Answer
Make enemy more intelligent 0 Answers
How to reach multiple GameObjects' value , enemy AI 1 Answer
how to make an enemy Ai blow up when you shoot it? 2 Answers
Enemy Color Change Help! 1 Answer