- Home /
Velocity and AddForce Problems
I'm hoping someone can reveal what I'm doing wrong in my script that I'm using to instantiate a weapon's projectile and set a velocity (or force). What's really strange is that I have almost this exact code working on another "enemy" and it's working fine.
I've tried with both "weaponClone.velocity" and "weaponClone.rigidbody.AddForce" but neither apply any motion to the projectile. Projectiles are instantiated no problem and otherwise act as I expect.
/* This script controls the enemy's weapon firing. It instantiates the projectile and puts it into motion. Usually towards the Player's location. */
// Require the weapon to be a rigidbody, That way we know a prefab cannot be assigned without rigidbody var weapon : Rigidbody; // We'll likely want to fire at the Player, so lets create a variable so we can locate the player var player : GameObject; // Specify how many seconds between each enemy weapon var fireRate = 1.00; // Tells us how many weapons have been fired by the enemy (not total, just since last fireRate) var fired = 0; // This variable will hold the time.time + time between firing. private var nextFire = 0.0; // Count the total number of weapons fired by this Enemy var totalWeaponsFired : float = 0; // Set the speed that the weapon travels at var weaponSpeed = 100; // Set the weapons to be disabled in the beginning private var weaponsActive = false;
// Cache the transform private var myTransform : Transform; function Awake () { myTransform = transform; }
function Start () { // Set the 'player' variable to find the Player's ship player = GameObject.Find("Ship"); }
function FireWeapon () { // Instantiate the weapon and start it's position at the same location as the object holding this script var weaponClone : Rigidbody = Instantiate(weapon, myTransform.position, myTransform.rotation); // Turn off gravity to this object's rigidbody weaponClone.rigidbody.useGravity = false; // If there's no collider and we need to add one, we use this... weaponClone.transform.gameObject.AddComponent(BoxCollider); // or MeshCollider // If we use the box collider, we need to specify a size for it. weaponClone.collider.size = Vector3(0.005,0.005,0.005); // Set the collider so that it's a trigger. Don't want the weapon bouncing off stuff weaponClone.collider.isTrigger = true; // Set a name for the instantiated Enemy weapon weaponClone.name = "VTOL Weapon"; // Set the speed and direction of the weapon //weaponClone.rigidbody.AddForce(myTransform.forward weaponSpeed); //weaponClone.velocity = (player.transform.position - transform.position).normalized weaponSpeed; weaponClone.velocity = (player.transform.position - transform.position)* weaponSpeed;
// Keep count of the total number of weapons fired by the Enemy
totalWeaponsFired++;
}
function FixedUpdate () { // Lets see if the Player even exists. No point firing if they're dead. if(player) { // Now lets see if the weapons are active if(weaponsActive == true) { // See if we have already fired if(fired <= 0) { // Enemy hasn't fired yet so they should fire a weapon FireWeapon(); // update the time (in seconds) when the Enemy can fire again nextFire = Time.time + fireRate; fired++; } // Enemy has fired if(nextFire <= Time.time) { // Reset the number of currently fired weapons fired = 0; } } } }
function WeaponsEnable() { weaponsActive = true; }
function WeaponsDisable() { weaponsActive = false; }
Oh, I should point out that function WeaponsEnable() is called by another script, so the weaponsActive variable is indeed true. Weapons are fired, they just don't move. :)