Search Unity

  1. The 2022.1 beta is now available for testing. To find out what's new, have a look at our 2022.1 beta blog post.
    Dismiss Notice

Instantiate with rotation?

Discussion in 'Scripting' started by stuthemoo, Aug 31, 2011.

  1. stuthemoo

    stuthemoo

    Joined:
    Sep 17, 2005
    Posts:
    182
    Hi, I'm trying to shoot a projectile, I can make it work with the following code. But the only problem is I want it to spin as it flys through the air and my last line of code doesn't seem to be working. How can I modify the following to make it spin on its z axis while flying?

    Code (csharp):
    1. var projectile : Rigidbody;
    2.  
    3. function Update () {
    4.     if (Input.GetButtonDown("Fire1")) {
    5.         var clone : Rigidbody;
    6.         clone = Instantiate(projectile, transform.position, transform.rotation);
    7.         clone.velocity = transform.TransformDirection (Vector3.forward * 30);
    8. // This is where I need help:
    9.         clone.angularVelocity = (Vector3.forward * Time.deltaTime);
    10.     }
    11. }
    Thanks a lot,
    Stuart
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,326
    Vector3.forward is the world's Z axis. transform.forward is the local Z axis. But you should AddTorque instead.
     
  3. stuthemoo

    stuthemoo

    Joined:
    Sep 17, 2005
    Posts:
    182
    Oh right. Thanks Jessy! But why should I add torque instead?
     
  4. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,326
    Use functions that make physical sense as much as you can when scripting physics. Other forces can stop your angularVelocity from ever being met, but the torque would exist at that moment no matter what.
     
unityunity