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. Dismiss Notice

How to set properly the mass center?

Discussion in 'Physics' started by Gorkatan, Mar 11, 2016.

  1. Gorkatan

    Gorkatan

    Joined:
    Sep 6, 2015
    Posts:
    12
    Hello,

    I'm trying to lauch a shell from a bazooka, in some kind of FPS. The shell should follow a parabolic path depending on the angle of the weapon. To do that I'm launching a GameObject with a shell-like Mesh which has got a Rigidbody affected by gravity. I used the following lines to launch the shell:

    Code (CSharp):
    1. GameObject newProjectile = Instantiate(Shell, FakeShell.transform.position, FakeShell.transform.rotation) as GameObject;
    2.             FakeShell.SetActive(false);
    Code (CSharp):
    1.  
    2. newProjectile.GetComponent<Rigidbody>().AddForce(FakeShell.transform.forward * ShellPower, ForceMode.VelocityChange);
    Where:
    Shell:GameObject to be launched.
    FakeShell: Shell that appear or disappear at the end ot the bazooka's tube depending on if it has be shot or not.
    ShellPower: Speed of shell while launch (25f).

    With this, the shell do follow a parabolic path, but it is not realistic at all because the it lands in the same positon as it is launched (head up), you can see what I mean in the pictures below.

    Rocket2.jpg Rocket3.jpg
    Rocket4.jpg

    But normally shells land head down.To achieve this I tried to modify the mass center ot the shell. So I added an empty gameobject exactly in the "head" of the shell (Which is called in the script HeadShell). And I added a script to the shell with the following lines in the Start() function:

    Code (CSharp):
    1. Rigidbody mRigidbody = GetComponent<Rigidbody>();
    2.  
    3. Vector3 CenterOfMass = HeadShelll.transform.position;
    4.  
    5. mRigidbody.centerOfMass = CenterOfMass;

    However this didn't modifed the behavior of the shell, it still lands as in the images above. What am I doing wrong? Or what could I do to make the head of the shell be heavier to fall down like in the following image:

    Rocket5.jpg

    Thank you very much for your feedback in advance.


    PS: English is not my first language, I'm not sure if the term: head of the shell is correct, but I tried to refer the front part of the shell.
     
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,418
    The center of mass is not the problem here. Real projectiles like that keep pointing in the direction of their velocity because of their aerodynamic properties. You could emulate this behavior by explicitly rotating your projectile to point in the direction of its velocity. Something like this in the projectile object (not actually tested):

    Code (CSharp):
    1. void FixedUpdate ()
    2.     {
    3.     if (mRigidbody.velocity.magnitude > 0.1f)
    4.        {
    5.        Quaternion rot = Quaternion.LookRotation(mRigidbody.velocity);
    6.        mRigidbody.MoveRotation(rot);
    7.        }
    8.     }
    EDIT: Fixed mistake on the "if" condition
     
    Last edited: Mar 11, 2016
    Gorkatan likes this.
  3. Gorkatan

    Gorkatan

    Joined:
    Sep 6, 2015
    Posts:
    12
    Than you very much Edy, I didn't know about it.
     
  4. Marcos-Schultz

    Marcos-Schultz

    Joined:
    Feb 24, 2014
    Posts:
    381
    "mRigidbody.magnitude" ????
     
  5. Gorkatan

    Gorkatan

    Joined:
    Sep 6, 2015
    Posts:
    12
    Well as I understood it's just to check if exist a Rigidbody. Instead I that line I wrote:

    Code (CSharp):
    1. if (mRigidbody)
    The rest worked out perfectly
     
  6. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,418
    Sorry, I meant "rigidbody.velocity.magnitude > 0.1f". Checking for a minimum velocity is important, otherwise Quaternion.LookRotation may return a weird rotation when the velocity is close to zero (it logs an error if velocity is exactly zero).
     
    Marcos-Schultz likes this.