Search Unity

Questions, questions...

Discussion in 'Scripting' started by groovista, Feb 5, 2008.

  1. groovista

    groovista

    Joined:
    Feb 15, 2007
    Posts:
    21
    I'm making progress with my first project, often by looting the tutorials and examples for code and props :D Sometimes I even understand what I'm working with. However, I've got several questions, and am unsure how to proceed in this forum. Should I lay out each issue in a separate thread?

    Here's my first issue:

    I wrote a script and attached it to my rocket bike game object, such that when I hit the CTRL key, the rocket engines fire and push the bike. A particle system forms the exhaust. Trouble is, the script only works the right one; the left one sprays like a sparkler at all times. Also, the rocket thrust is into World-Z and not Object-Z, and all my twiddling with this is so far for nought.

    Thanks in advance!


    Code (csharp):
    1. // When the CTRL key is pressed, the rocket motors kick in and push the bike.
    2.  
    3. var power = 200.0;
    4.  
    5. function FixedUpdate () {
    6.  
    7.     var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter);
    8.  
    9.     if(Input.GetAxis("Fire1")) {
    10.        
    11.         rigidbody.AddForce (Vector3.forward * power);
    12.         emitter.emit = true;
    13.     }  
    14.     else {
    15.         emitter.emit = false;
    16.     }
    17. }
     

    Attached Files:

  2. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    GetComponentInChildren(...) only searches until it finds the first instance of whatever you are searching for, then it stops. So it will only ever do one.

    What you want to do is use GetComponentsInChildren(...) (notice the 's'). What that will do is return all instances of that component type that is attached to this object in an array.

    Then you can operate each returned component separately.

    HTH,
    -Jeremy
     
  3. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    On a separate note, I recommend referencing the two particle emitters directly. With the way you are doing it now, if you add some additional particle effect, this code will screw up.

    Use public inspector vars, and drag your emitters over manually.

    -Jeremy
     
  4. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    What jeremy said. Nice particle effect!

    Also, try using rigidbody.AddRelativeForce() instead.
     
  5. groovista

    groovista

    Joined:
    Feb 15, 2007
    Posts:
    21
    Very much obliged, gents!

    @jeremy: my spellweaving is still limited :) How do I properly implement the array you suggested?

    @yoggy: Does rigidbody.addRelativeForce correct the thrust direction? Thanks, you Da Man on particles; I studied your tutorial for tips. I figure in space, nobody knows you smoke ;-)

    Code (csharp):
    1. // When the CTRL key is pressed, the rocket motors kick in and push the bike.
    2. // Thanks to jeremeyace  yoggy for tips!
    3.  
    4. var rockets = new Array ();
    5. var power = 200.0;
    6.  
    7. function FixedUpdate () {
    8.  
    9.     var emitter : ParticleEmitter = GetComponentsInChildren(RocketBike);
    10.  
    11.     if(Input.GetAxis("Fire1")) {
    12.        
    13.         rigidbody.AddRelativeForce (Vector3.forward * power);
    14.         emitter.emit = true;
    15.     }  
    16.     else {
    17.         emitter.emit = false;
    18.     }
    19. }
     
  6. ChromeFly

    ChromeFly

    Joined:
    Nov 4, 2007
    Posts:
    60
    Code (csharp):
    1. // When the CTRL key is pressed, the rocket motors kick in and push the bike.
    2. // Thanks to jeremeyace  yoggy for tips!
    3.  
    4. var lRocket : ParticleEmitter;
    5. var rRocket : ParticleEmitter;
    6. var power = 200.0;
    7.  
    8. function FixedUpdate () {
    9.  
    10.    //var emitter : ParticleEmitter = GetComponentsInChildren(RocketBike);
    11.  
    12.    if(Input.GetAxis("Fire1")) {
    13.      
    14.       rigidbody.AddRelativeForce (Vector3.forward * power);
    15.       lRocket.emit = true;
    16.       rRocket.emit = true;
    17.    }  
    18.    else {
    19.       lRocket.emit = false;
    20.       rRocket.emit = false;
    21.    }
    22. }
    Drag your Particle Emitters onto the relevant exposed slots in the inspector.

    If you use GetComponentsInChildren(...) to get an array of all the Particle Emitters, it will begin acting wacky if you happen to add more particles to the object because the array will include ALL emitters, not just the rocket ones.

    What Jeremy said... :)
     
  7. groovista

    groovista

    Joined:
    Feb 15, 2007
    Posts:
    21
    Ahhh...more better indeed! (See the latest build, and thank you for the example code!)

    This technique, then, will permit proper control of the maneuvering thrusters without firing everything at once, ¿sí?

    I will reflect upon this...
     
  8. groovista

    groovista

    Joined:
    Feb 15, 2007
    Posts:
    21
    Progress, hoorrah!

    Now, why is the Main Camera "juddering"? The RocketBike shakes and twitches a little, but the camera shake seems most pronounced in the area between the first and second jumps. I can't yet tell if it's the camera's target that's shaking, or something in the script that's interacting oddly.

    I used the Smooth Follow.js script from the Race Demo example:

    Code (csharp):
    1. /*
    2. This camera smoothes out rotation around the y-axis and height.
    3. Horizontal Distance to the target is always fixed.
    4.  
    5. There are many different ways to smooth the rotation but doing it this way gives you a lot of control over how the camera behaves.
    6.  
    7. For every of those smoothed values we calculate the wanted value and the current value.
    8. Then we smooth it using the Lerp function.
    9. Then we apply the smoothed values to the transform's position.
    10. */
    11.  
    12. // The target we are following
    13. var target : Transform;
    14. // The distance in the x-z plane to the target
    15. var distance = 10.0;
    16. // the height we want the camera to be above the target
    17. var height = 5.0;
    18. // How much we
    19. var heightDamping = 2.0;
    20. var rotationDamping = 3.0;
    21.  
    22. // Place the script in the Camera-Control group in the component menu
    23. @AddComponentMenu("Camera-Control/Smooth Follow")
    24. partial class SmoothFollow { }
    25.  
    26.  
    27. function LateUpdate () {
    28.     // Early out if we don't have a target
    29.     if (!target)
    30.         return;
    31.    
    32.     // Calculate the current rotation angles
    33.     wantedRotationAngle = target.eulerAngles.y;
    34.     wantedHeight = target.position.y + height;
    35.        
    36.     currentRotationAngle = transform.eulerAngles.y;
    37.     currentHeight = transform.position.y;
    38.    
    39.     // Damp the rotation around the y-axis
    40.     currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);
    41.  
    42.     // Damp the height
    43.     currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);
    44.  
    45.     // Convert the angle into a rotation
    46.     // The quaternion interface uses radians not degrees so we need to convert from degrees to radians
    47.     currentRotation = Quaternion.Euler (0, currentRotationAngle * Mathf.Deg2Rad, 0);
    48.    
    49.     // Set the position of the camera on the x-z plane to:
    50.     // distance meters behind the target
    51.     transform.position = target.position;
    52.     transform.position -= currentRotation * Vector3.forward * distance;
    53.  
    54.     // Set the height of the camera
    55.     transform.position.y = currentHeight;
    56.    
    57.     // Always look at the target
    58.     transform.LookAt (target);
    59. }
     

    Attached Files: