0
\$\begingroup\$

I want to have a projectile that the character fires stay in the same place for a second or two before flying off in the direction of the mouse at the initial time of fire. The character can move away from the projectile after firing it, and the projectile wont move till after the time has passed.

\$\endgroup\$
  • \$\begingroup\$ What have you tried so far? What is your current code used to shoot your bullet? \$\endgroup\$ – Hellium Oct 30 '17 at 9:01
1
\$\begingroup\$

Sounds like a job for Coroutines....

So you start off with something like this :

 IEnumerator WaitingBullet( float waitTime)
    {
        //Spawn bullet and set direction
        yield return new WaitForSeconds(waitTime);
        // give the bullet some velocity to fly off in its set direction
        yield return null;
    }

And then you use this to start the coroutine when you detect a fire input :

StartCoroutine(WaitingBullet(2.0f));

This will wait for 2 seconds between spawning the bullet and setting it's velocity. Hope this sets you on the right track.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

You would have to use a coroutine. The usage of a coroutine looks something like this:

IEnumerator Wait()
{
    yield return new Waitforseconds(1f);// wait for 1 sec
    Shoot();// shoot your projectile
}

In order to use coroutines you have to say at the top of the script: using UnityEngine.System.Collections

To leave the bullet steady, you could unparent the bullet from the gun. Look for Gameobject.SetParent() or something similar on the Unity API

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.