/// <summary> /// This method is used to assign traits to our missle assigned from the launcher. /// </summary> public void AssignMissleRules(Missile_Launcher.MissileType missileType, Transform target, float launchSpeed, float power, float fuseDelay, float destroyTimer) { _missileType = missileType; //assign the missle type _target = target; //Who should the rocket follow? _launchSpeed = launchSpeed; //set the launch speed _power = power; //set the power _fuseDelay = fuseDelay; //set the fuse delay Destroy(this.gameObject, destroyTimer); //destroy the rocket after destroyTimer }
// Update is called once per frame void FixedUpdate() { if (_fuseOut == false) //check if fuseOut is false { return; } if (_launched == true) //check if launched is true { _rigidbody.AddForce(transform.forward * _launchSpeed); //add force to the rocket in the forward direction if (Time.time > _initialLaunchTime + _fuseDelay) //check if the initial launch + fuse delay has passed { _launched = false; //launched bool goes false _thrust = true; //thrust bool goes true } } if (_thrust == true) //if thrust is true { _rigidbody.useGravity = true; //enable gravity _rigidbody.velocity = transform.forward * _power; //set velocity multiplied by the power variable _thrust = false; //set thrust bool to false _trackRotation = true; //track rotation bool set to true } if (_trackRotation == true) //check track rotation bool { if (_missileType == Missile_Launcher.MissileType.Normal) //checking for normal missile { _rigidbody.rotation = Quaternion.LookRotation(_rigidbody.velocity); // adjust rotation of rocket based on velocity _rigidbody.AddForce(transform.forward * 100f); //add force to the rocket } else if (_missileType == Missile_Launcher.MissileType.Homing) //if missle is homing { if (_target == null) //checking if the target is null { _missileType = Missile_Launcher.MissileType.Normal; //assign back to normal return; } Vector3 direction = _target.position - transform.position; //calculate direciton for rocket to face direction.Normalize(); //set the magnitude of the vector to 1 Vector3 turnAmount = Vector3.Cross(transform.forward, direction); //using cross product, we multiply our forward vector of the rocket by the direction vector, to create a perpendular vector, which specifies the turn amount _rigidbody.angularVelocity = turnAmount * _power; //apply angular velocity _rigidbody.velocity = transform.forward * _power; //apply forward velocity } } }