Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Bomb Dropping Problem - Want Bomb to take plane's velocity only on instantiation

Discussion in 'Scripting' started by The-NightflyAZ, Oct 3, 2017.

  1. The-NightflyAZ

    The-NightflyAZ

    Joined:
    Apr 27, 2015
    Posts:
    17
    Howdy ho everyone! I have a bomb dropping problem that I’m sure is not too difficult to solve but for some reason I just can’t seem to get a working solution.

    What I’m trying to do is instantiate a bomb from an airplane and I want the bomb to take the airplane’s velocity but only on the frame that it is instantiated. After the bomb is created I want the physics engine to just take over and make the bomb’s rigid body just basically drop like it would in the real world.

    What is happening is that I can get the bomb to instantiate and take, in this case, the plane’s velocity, but the plane’s velocity keeps getting plugged into the bomb’s velocity so when I pull up out of the dive bombing run, the bomb changes direction. Click the link below to see a video example of what I’m talking about.

    Bomb dropping problem video explanation

    Here’s the relevant code. I’ve basically added to the Aeroplane controller from the Standard assets.
    void Update()
    {

    if (Input.GetKeyDown (KeyCode.LeftShift))
    {
    DropBomb ();
    }
    }

    public void DropBomb()
    {
    Vector3 rigidBombVelocity = m_Rigidbody.velocity; //get a new vector3 of the plane's velocity at time method is run...

    Rigidbody newbomb =Instantiate (rigidBomb, rigidBombSpawnPoint.position, rigidBombSpawnPoint.rotation) as Rigidbody; //instantiating the new bom

    newbomb.velocity = rigidBombVelocity; //setting the new bomb to have the velocity of the plane when method is run

    }

    Thanks in advance!
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    It shouldn't be inheriting velocity. The code you've got should only assign the velocity once.
     
  3. The-NightflyAZ

    The-NightflyAZ

    Joined:
    Apr 27, 2015
    Posts:
    17
    I thought my code was good but looking at the visuals when playing the bomb motion just looks wrong. I'm going to check what I'm doing again w/a 2nd fixed camera so I can see the bomb relative to the plane and maybe do a debug log to check for the bomb's velocity.
     
  4. The-NightflyAZ

    The-NightflyAZ

    Joined:
    Apr 27, 2015
    Posts:
    17
    I'll be god damned. Once I pulled the camera away from the plane so I could see both the plane and the bomb's relative motion I realized it is working. I've been thinking it hasn't been working for about the last two weeks and pulling my hair out trying to find a solution! I should have done what instructors tell you when figure drawing...every few minutes stop drawing, stand back and take a look at your drawing from a distance. Then it's easier to see if something doesn't look right...
     
    Kiwasi likes this.