- Home /
Can't get bullet to spawn and move
Doing a certificate course in Game Development and the code that we've been given hasn't been working properly for me, mainly the projectile firing script for the main player and the enemies, this here is for the enemies yet, the main player's script for firing is relatively the same, The main problem I'm have here is that the enemy doesn't actually fire until it's in contact with me and when it is it spawns continous copies of projectile that are invisible yet in the hierachy. The main player I can't even get the projectile to appear in the hierachy at all\ Could I get some help please guys :D!
using UnityEngine;
using System.Collections;
public class Enemy : MonoBehaviour {
public GameObject ExplosionType;
public float RespawnTimer = 10.0f;
public int Health = 100;
public float StopDistance = 5.0f;
public float MovementSpeed = 5.0f;
public float ProjectileForwardForce = 10;
public float ProjectileUpForce = 10;
public GameObject objectToSpawn;
float CurrentTimer;
GameObject ParticleExplosion;
bool isAlive = true;
// Use this for initialization
void Start () {
CurrentTimer = RespawnTimer;
}
// Update is called once per frame
void Update () {
if ( isAlive == false)
{
CurrentTimer -= Time.deltaTime;
if (CurrentTimer < 0)
{
Destroy( ParticleExplosion );
Destroy ( gameObject );
}
}
else
{
if(Health <= 0)
{
Explode();
}
GameObject tank = GameObject.FindGameObjectWithTag("Player");
if (tank != null)
{
// work out new facing direction for tank
Quaternion rotation = Quaternion.LookRotation(tank.transform.position - transform.position);
// slowly rotate to new facing
transform.rotation = Quaternion.Slerp (transform.rotation, rotation, Time.deltaTime);
//get distance between enemy and player for when to stop
float distance = Vector3.Distance(tank.transform.position, transform.position);
if (distance > StopDistance)
{
transform.Translate (transform.forward * MovementSpeed * Time.deltaTime);
}
else
{
Vector3 offsetForward = -transform.up * 1;
Vector3 offsetUp = transform.forward * 0.5f;
Vector3 finalPosition = transform.position + offsetForward + offsetUp;
GameObject projectile = Instantiate(objectToSpawn, finalPosition, transform.rotation) as GameObject;
projectile.rigidbody.AddRelativeForce(projectile.transform.forward * -ProjectileForwardForce, ForceMode.Impulse);
projectile.rigidbody.AddRelativeForce(projectile.transform.up * -ProjectileUpForce, ForceMode.Impulse);
}
}
}
}
public void Hit(int Damage)
{
Health -= Damage;
}
/// Explode this instance.
public void Explode ()
{
ParticleExplosion = Instantiate(ExplosionType, transform.position, transform.rotation) as GameObject;
isAlive = false;
}
}
Answer by robertbu · Apr 04, 2013 at 01:10 PM
A few suggestions. You appear to be firing backwards (I have to guess at your geometry). You are using '-ProjectileFordwardForce' and '-ProjectileUpForce' instead of positive values. In addition, if the mass is set to 1.0, you are applying far too little force (try 500 or 1000). There may be a reason, but I've never seen an independent 'up' force. Usually the gun is aimed, and the projectile is fired with a simple forward force to arch the projectile towards the target. Since I don't see any gun elevation, maybe you need this.
Also calculating 'offsetForward + offsetUp' is unusual (and you appear to be doing backwards though it should not matter given the way you use the values). Usually there is an empty game object placed in front of the gun and made a child of the gun. This empty game object is used for position and often rotation of the projectile, and provides a way in scene view to verify the projectile is shooting from the right place (and not hitting any internal colliders).
Your answer
Follow this Question
Related Questions
GuiButton Going To Next Level 0 Answers
Script not working 0 Answers
Menu script not showing up like normal 1 Answer