Search Unity

Velocity 'pause' after Instantiation

Discussion in 'Scripting' started by marraboy, Mar 29, 2008.

  1. marraboy

    marraboy

    Joined:
    Mar 25, 2008
    Posts:
    113
    Hi

    Not sure if this is a script issue (I guess there is a property I am missing)?

    I have an asteroid clone game I am 'experimenting' with and I decided to add missiles/rockets.

    I use the following scripts:

    Code (csharp):
    1.  
    2. // attached to spaceship prefab
    3. public class MissileManager : MonoBehaviour {
    4.  
    5.  public Rigidbody rocket;
    6.  
    7.  void FireRocket () {
    8.      Instantiate(rocket, rigidbody.position, rigidbody.rotation);
    9.  }
    10.  
    11.  void Update () {
    12.   if(Input.GetButtonDown("Fire1")){
    13.    FireRocket();
    14.   }
    15.  }
    16. }
    17.  
    18. // attached to rocket prefab
    19. public class RocketBehaviour : MonoBehaviour {
    20.  
    21.  public float MissileSpeed = 10.0f;
    22.  
    23.  void Start () {
    24.   rigidbody.AddRelativeForce(Vector3.forward * MissileSpeed);
    25.  }
    26. }
    27.  
    My problem is that when the fire key is pressed the missile/rocket is instantiated correctly but pauses for a breif moment (about half a second) before the velocity is applied.

    Any help/insight would be much appreciated.

    JT
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You might try:

    Code (csharp):
    1.  void FireRocket () {
    2.      Rigidbody rocket = Instantiate(rocket, rigidbody.position, rigidbody.rotation);
    3.      rocket.AddRelativeForce(Vector3.forward * MissileSpeed);
    4.  }
    That way the rocket will be instantiated and have force added the same frame. Not sure why you'd get that much of a pause the other way though.

    --Eric
     
  3. marraboy

    marraboy

    Joined:
    Mar 25, 2008
    Posts:
    113
    Hi Eric

    Thanks for the reply. Yeah I also tried that but just the same!

    What I finally did was a bit of non-sense... I removed the Rocket prefab and Rocket model. Re-imported, recreated theprefab and added the script components and it worked!

    I have no idea why?!? Possibly I constructed the first prefab incorrectly.

    JT