I'm making progress with my first project, often by looting the tutorials and examples for code and props 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): // When the CTRL key is pressed, the rocket motors kick in and push the bike. var power = 200.0; function FixedUpdate () { var emitter : ParticleEmitter = GetComponentInChildren(ParticleEmitter); if(Input.GetAxis("Fire1")) { rigidbody.AddForce (Vector3.forward * power); emitter.emit = true; } else { emitter.emit = false; } }
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
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
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): // When the CTRL key is pressed, the rocket motors kick in and push the bike. // Thanks to jeremeyace yoggy for tips! var rockets = new Array (); var power = 200.0; function FixedUpdate () { var emitter : ParticleEmitter = GetComponentsInChildren(RocketBike); if(Input.GetAxis("Fire1")) { rigidbody.AddRelativeForce (Vector3.forward * power); emitter.emit = true; } else { emitter.emit = false; } }
Code (csharp): // When the CTRL key is pressed, the rocket motors kick in and push the bike. // Thanks to jeremeyace yoggy for tips! var lRocket : ParticleEmitter; var rRocket : ParticleEmitter; var power = 200.0; function FixedUpdate () { //var emitter : ParticleEmitter = GetComponentsInChildren(RocketBike); if(Input.GetAxis("Fire1")) { rigidbody.AddRelativeForce (Vector3.forward * power); lRocket.emit = true; rRocket.emit = true; } else { lRocket.emit = false; rRocket.emit = false; } } 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...
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...
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): /* This camera smoothes out rotation around the y-axis and height. Horizontal Distance to the target is always fixed. 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. For every of those smoothed values we calculate the wanted value and the current value. Then we smooth it using the Lerp function. Then we apply the smoothed values to the transform's position. */ // The target we are following var target : Transform; // The distance in the x-z plane to the target var distance = 10.0; // the height we want the camera to be above the target var height = 5.0; // How much we var heightDamping = 2.0; var rotationDamping = 3.0; // Place the script in the Camera-Control group in the component menu @AddComponentMenu("Camera-Control/Smooth Follow") partial class SmoothFollow { } function LateUpdate () { // Early out if we don't have a target if (!target) return; // Calculate the current rotation angles wantedRotationAngle = target.eulerAngles.y; wantedHeight = target.position.y + height; currentRotationAngle = transform.eulerAngles.y; currentHeight = transform.position.y; // Damp the rotation around the y-axis currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime); // Damp the height currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime); // Convert the angle into a rotation // The quaternion interface uses radians not degrees so we need to convert from degrees to radians currentRotation = Quaternion.Euler (0, currentRotationAngle * Mathf.Deg2Rad, 0); // Set the position of the camera on the x-z plane to: // distance meters behind the target transform.position = target.position; transform.position -= currentRotation * Vector3.forward * distance; // Set the height of the camera transform.position.y = currentHeight; // Always look at the target transform.LookAt (target); }