Search Unity

Fart?

Discussion in 'Scripting' started by TheCowMan, Sep 13, 2010.

  1. TheCowMan

    TheCowMan

    Joined:
    Aug 24, 2010
    Posts:
    387
    I am making a game where you can fart to fly around. Kinda like a boost. I am using the 2d platformer rocket script but it's really hard to figure this out. I want to do, var Gas = 100; at start. Then as you boost, minus 5 every time you "Fart" Then if you have Gas = 0;
    you cant fart untill you pick up Beans. Could someone tell me where to put all this?

    Code (csharp):
    1.  
    2.  
    3.  
    4. // Does this script currently respond to Input?
    5. var canControl = true;
    6.  
    7. // The character will spawn at spawnPoint's position when needed.  This could be changed via a script at runtime to implement, e.g. waypoints/savepoints.
    8. var spawnPoint : Transform;
    9.  
    10. class PlatformerControllerMovement {
    11.     // The speed when walking
    12.     var walkSpeed = 3.0;
    13.     // when pressing "Fire1" button (control) we start running
    14.     var runSpeed = 10.0;
    15.  
    16.     var inAirControlAcceleration = 1.0;
    17.  
    18.     // The gravity for the character
    19.     var gravity = 60.0;
    20.     var maxFallSpeed = 20.0;
    21.  
    22.     // How fast does the character change speeds?  Higher is faster.
    23.     var speedSmoothing = 5.0;
    24.  
    25.     // This controls how fast the graphics of the character "turn around" when the player turns around using the controls.
    26.     var rotationSmoothing = 10.0;
    27.  
    28.     // The current move direction in x-y.  This will always been (1,0,0) or (-1,0,0)
    29.     // The next line, @System.NonSerialized , tells Unity to not serialize the variable or show it in the inspector view.  Very handy for organization!
    30.     @System.NonSerialized
    31.     var direction = Vector3.zero;
    32.  
    33.     // The current vertical speed
    34.     @System.NonSerialized
    35.     var verticalSpeed = 0.0;
    36.  
    37.     // The current movement speed.  This gets smoothed by speedSmoothing.
    38.     @System.NonSerialized
    39.     var speed = 0.0;
    40.  
    41.     // Is the user pressing the left or right movement keys?
    42.     @System.NonSerialized
    43.     var isMoving = false;
    44.  
    45.     // The last collision flags returned from controller.Move
    46.     @System.NonSerialized
    47.     var collisionFlags : CollisionFlags;
    48.  
    49.     // We will keep track of an approximation of the character's current velocity, so that we return it from GetVelocity () for our camera to use for prediction.
    50.     @System.NonSerialized
    51.     var velocity : Vector3;
    52.    
    53.     // This keeps track of our current velocity while we're not grounded?
    54.     @System.NonSerialized
    55.     var inAirVelocity = Vector3.zero;
    56.  
    57.     // This will keep track of how long we have we been in the air (not grounded)
    58.     @System.NonSerialized
    59.     var hangTime = 0.0;
    60. }
    61.  
    62. var movement : PlatformerControllerMovement;
    63.  
    64. // We will contain all the jumping related variables in one helper class for clarity.
    65. class PlatformerControllerJumping {
    66.     // Can the character jump?
    67.     var enabled = true;
    68.  
    69.     // How high do we jump when pressing jump and letting go immediately
    70.     var height = 1.0;
    71.     // We add extraHeight units (meters) on top when holding the button down longer while jumping
    72.     var extraHeight = 4.1;
    73.    
    74.     // This prevents inordinarily too quick jumping
    75.     // The next line, @System.NonSerialized , tells Unity to not serialize the variable or show it in the inspector view.  Very handy for organization!
    76.     @System.NonSerialized
    77.     var repeatTime = 0.05;
    78.  
    79.     @System.NonSerialized
    80.     var timeout = 0.15;
    81.  
    82.     // Are we jumping? (Initiated with jump button and not grounded yet)
    83.     @System.NonSerialized
    84.     var jumping = false;
    85.    
    86.     @System.NonSerialized
    87.     var reachedApex = false;
    88.  
    89.     // Last time the jump button was clicked down
    90.     @System.NonSerialized
    91.     var lastButtonTime = -10.0;
    92.    
    93.     // Last time we performed a jump
    94.     @System.NonSerialized
    95.     var lastTime = -1.0;
    96.  
    97.     // the height we jumped from (Used to determine for how long to apply extra jump power after jumping.)
    98.     @System.NonSerialized
    99.     var lastStartHeight = 0.0;
    100. }
    101.  
    102. var jump : PlatformerControllerJumping;
    103.  
    104. private var controller : CharacterController;
    105.  
    106. // Moving platform support.
    107. private var activePlatform : Transform;
    108. private var activeLocalPlatformPoint : Vector3;
    109. private var activeGlobalPlatformPoint : Vector3;
    110. private var lastPlatformVelocity : Vector3;
    111.  
    112. // This is used to keep track of special effects in UpdateEffects ();
    113. private var areEmittersOn = false;
    114.  
    115. function Awake () {
    116.     movement.direction = transform.TransformDirection (Vector3.forward);
    117.     controller = GetComponent (CharacterController);
    118.     Spawn ();
    119. }
    120.  
    121. function Spawn () {
    122.     // reset the character's speed
    123.     movement.verticalSpeed = 0.0;
    124.     movement.speed = 0.0;
    125.    
    126.     // reset the character's position to the spawnPoint
    127.     transform.position = spawnPoint.position;
    128.    
    129. }
    130.  
    131. function OnDeath () {
    132.     Spawn ();
    133. }
    134.  
    135. function UpdateSmoothedMovementDirection () {  
    136.     var h = Input.GetAxisRaw ("Horizontal");
    137.    
    138.     if (!canControl)
    139.         h = 0.0;
    140.    
    141.     movement.isMoving = Mathf.Abs (h) > 0.1;
    142.        
    143.     if (movement.isMoving)
    144.         movement.direction = Vector3 (h, 0, 0);
    145.    
    146.     // Grounded controls
    147.     if (controller.isGrounded) {
    148.         // Smooth the speed based on the current target direction
    149.         var curSmooth = movement.speedSmoothing * Time.deltaTime;
    150.        
    151.         // Choose target speed
    152.         var targetSpeed = Mathf.Min (Mathf.Abs(h), 1.0);
    153.    
    154.         // Pick speed modifier
    155.         if (Input.GetButton ("Fire2")  canControl)
    156.             targetSpeed *= movement.runSpeed;
    157.         else
    158.             targetSpeed *= movement.walkSpeed;
    159.        
    160.         movement.speed = Mathf.Lerp (movement.speed, targetSpeed, curSmooth);
    161.        
    162.         movement.hangTime = 0.0;
    163.     }
    164.     else {
    165.         // In air controls
    166.         movement.hangTime += Time.deltaTime;
    167.         if (movement.isMoving)
    168.             movement.inAirVelocity += Vector3 (Mathf.Sign(h), 0, 0) * Time.deltaTime * movement.inAirControlAcceleration;
    169.     }
    170. }
    171.  
    172. function FixedUpdate () {
    173.     // Make sure we are absolutely always in the 2D plane.
    174.     transform.position.z = 0;
    175.  
    176. }
    177.  
    178. function ApplyJumping () {
    179.     // Prevent jumping too fast after each other
    180.     if (jump.lastTime + jump.repeatTime > Time.time)
    181.         return;
    182.  
    183.     if (controller.isGrounded) {
    184.         // Jump
    185.         // - Only when pressing the button down
    186.         // - With a timeout so you can press the button slightly before landing    
    187.         if (jump.enabled  Time.time < jump.lastButtonTime + jump.timeout) {
    188.             movement.verticalSpeed = CalculateJumpVerticalSpeed (jump.height);
    189.             movement.inAirVelocity = lastPlatformVelocity;
    190.             SendMessage ("DidJump", SendMessageOptions.DontRequireReceiver);
    191.         }
    192.     }
    193. }
    194.  
    195. function ApplyGravity () {
    196.     // Apply gravity
    197.     var jumpButton = Input.GetButton ("Jump");
    198.    
    199.     if (!canControl)
    200.         jumpButton = false;
    201.    
    202.     // When we reach the apex of the jump we send out a message
    203.     if (jump.jumping  !jump.reachedApex  movement.verticalSpeed <= 0.0) {
    204.         jump.reachedApex = true;
    205.         SendMessage ("DidJumpReachApex", SendMessageOptions.DontRequireReceiver);
    206.     }
    207.    
    208.     // * When jumping up we don't apply gravity for some time when the user is holding the jump button
    209.     //   This gives more control over jump height by pressing the button longer
    210.     var extraPowerJump =  jump.jumping  movement.verticalSpeed > 0.0  jumpButton  transform.position.y < jump.lastStartHeight + jump.extraHeight  !IsTouchingCeiling ();
    211.    
    212.     if (extraPowerJump)
    213.         return;
    214.     else if (controller.isGrounded)
    215.         movement.verticalSpeed = -movement.gravity * Time.deltaTime;
    216.     else
    217.         movement.verticalSpeed -= movement.gravity * Time.deltaTime;
    218.        
    219.     // Make sure we don't fall any faster than maxFallSpeed.  This gives our character a terminal velocity.
    220.     movement.verticalSpeed = Mathf.Max (movement.verticalSpeed, -movement.maxFallSpeed);
    221. }
    222.  
    223. function CalculateJumpVerticalSpeed (targetJumpHeight : float) {
    224.     // From the jump height and gravity we deduce the upwards speed
    225.     // for the character to reach at the apex.
    226.     return Mathf.Sqrt (2 * targetJumpHeight * movement.gravity);
    227. }
    228.  
    229. function DidJump () {
    230.     jump.jumping = true;
    231.     jump.reachedApex = false;
    232.     jump.lastTime = Time.time;
    233.     jump.lastStartHeight = transform.position.y;
    234.     jump.lastButtonTime = -10;
    235. }
    236.  
    237. function UpdateEffects () {
    238.     wereEmittersOn = areEmittersOn;
    239.     areEmittersOn = jump.jumping  movement.verticalSpeed > 0.0;
    240.    
    241.     // By comparing the previous value of areEmittersOn to the new one, we will only update the particle emitters when needed
    242.     if (wereEmittersOn != areEmittersOn) {
    243.         for (var emitter in GetComponentsInChildren (ParticleEmitter)) {
    244.             emitter.emit = areEmittersOn;
    245.         }
    246.     }
    247. }
    248.  
    249. function Update () {
    250.     if (Input.GetButtonDown ("Jump")  canControl) {
    251.         jump.lastButtonTime = Time.time;
    252.     }
    253.  
    254.     UpdateSmoothedMovementDirection();
    255.    
    256.     // Apply gravity
    257.     // - extra power jump modifies gravity
    258.     ApplyGravity ();
    259.  
    260.     // Apply jumping logic
    261.     ApplyJumping ();
    262.    
    263.     // Moving platform support
    264.     if (activePlatform != null) {
    265.         var newGlobalPlatformPoint = activePlatform.TransformPoint(activeLocalPlatformPoint);
    266.         var moveDistance = (newGlobalPlatformPoint - activeGlobalPlatformPoint);
    267.         transform.position = transform.position + moveDistance;
    268.         lastPlatformVelocity = (newGlobalPlatformPoint - activeGlobalPlatformPoint) / Time.deltaTime;
    269.     } else {
    270.         lastPlatformVelocity = Vector3.zero;   
    271.     }
    272.    
    273.     activePlatform = null;
    274.    
    275.     // Save lastPosition for velocity calculation.
    276.     lastPosition = transform.position;
    277.    
    278.     // Calculate actual motion
    279.     var currentMovementOffset = movement.direction * movement.speed + Vector3 (0, movement.verticalSpeed, 0) + movement.inAirVelocity;
    280.    
    281.     // We always want the movement to be framerate independent.  Multiplying by Time.deltaTime does this.
    282.     currentMovementOffset *= Time.deltaTime;
    283.    
    284.     // Move our character!
    285.     movement.collisionFlags = controller.Move (currentMovementOffset);
    286.    
    287.     // Calculate the velocity based on the current and previous position.  
    288.     // This means our velocity will only be the amount the character actually moved as a result of collisions.
    289.     movement.velocity = (transform.position - lastPosition) / Time.deltaTime;
    290.    
    291.     // Moving platforms support
    292.     if (activePlatform != null) {
    293.         activeGlobalPlatformPoint = transform.position;
    294.         activeLocalPlatformPoint = activePlatform.InverseTransformPoint (transform.position);
    295.     }
    296.    
    297.     // Set rotation to the move direction  
    298.     if (movement.direction.sqrMagnitude > 0.01)
    299.         transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation (movement.direction), Time.deltaTime * movement.rotationSmoothing);
    300.    
    301.     // We are in jump mode but just became grounded
    302.     if (controller.isGrounded) {
    303.         movement.inAirVelocity = Vector3.zero;
    304.         if (jump.jumping) {
    305.             jump.jumping = false;
    306.             SendMessage ("DidLand", SendMessageOptions.DontRequireReceiver);
    307.  
    308.             var jumpMoveDirection = movement.direction * movement.speed + movement.inAirVelocity;
    309.             if (jumpMoveDirection.sqrMagnitude > 0.01)
    310.                 movement.direction = jumpMoveDirection.normalized;
    311.         }
    312.     }  
    313.  
    314.     // Update special effects like rocket pack particle effects
    315.     UpdateEffects ();
    316. }
    317.  
    318. function OnControllerColliderHit (hit : ControllerColliderHit)
    319. {
    320.     if (hit.moveDirection.y > 0.01)
    321.         return;
    322.    
    323.     // Make sure we are really standing on a straight platform
    324.     // Not on the underside of one and not falling down from it either!
    325.     if (hit.moveDirection.y < -0.9  hit.normal.y > 0.9) {
    326.         activePlatform = hit.collider.transform;   
    327.     }
    328. }
    329.  
    330. // Various helper functions below:
    331. function GetSpeed () {
    332.     return movement.speed;
    333. }
    334.  
    335. function GetVelocity () {
    336.     return movement.velocity;
    337. }
    338.  
    339.  
    340. function IsMoving () {
    341.     return movement.isMoving;
    342. }
    343.  
    344. function IsJumping () {
    345.     return jump.jumping;
    346. }
    347.  
    348. function IsTouchingCeiling () {
    349.     return (movement.collisionFlags  CollisionFlags.CollidedAbove) != 0;
    350. }
    351.  
    352. function GetDirection () {
    353.     return movement.direction;
    354. }
    355.  
    356. function GetHangTime() {
    357.     return movement.hangTime;
    358. }
    359.  
    360. function Reset () {
    361.     gameObject.tag = "Player";
    362. }
    363.  
    364. function SetControllable (controllable : boolean) {
    365.     canControl = controllable;
    366. }
    367.  
    368. // Require a character controller to be attached to the same game object
    369. @script RequireComponent (CharacterController)
    370. @script AddComponentMenu ("2D Platformer/Platformer Controller")
    371.  
     
  2. grobm

    grobm

    Joined:
    Aug 15, 2005
    Posts:
    215
    You need to define if statement, if your value greater then 0 then do fart... other wise... no far... need beans.

    When you collide into a bean your value goes up X amount... now you can fart.

    Cheers, enjoy the gas.

    :)
     
  3. pavees

    pavees

    Joined:
    Jan 1, 2009
    Posts:
    116
    u can see a fart effect in the game " Beast Farmer " that is already ther in app store..is that wat u mean to do??
     
  4. raymix

    raymix

    Joined:
    Aug 25, 2010
    Posts:
    192
    do i feel another "Boogerman" title? xcept that guy also burped, and could fly by farting, if ate some chilly, lol
     
  5. chubbspet

    chubbspet

    Joined:
    Feb 18, 2010
    Posts:
    1,220
    How are you planning to code the smell:D
     
  6. mythicwave

    mythicwave

    Joined:
    Jul 13, 2008
    Posts:
    144
    Yes, I can tell you where to put all of it!

    Actually, I have no experience programming with farts. I'm better with burps.

    -- Brian
     
  7. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    of course put the gasCount=100.0 at the top outside any functions as well as a variable called spacePressed=false;

    In an OnCollisionEnter function put gasCount=gasCount+1;

    Also in the OnCollisionEnter you will want to put the Destroy(otherCollider.gameObject);

    In the Update() function you will need to put some code like this:

    Code (csharp):
    1.  
    2. if(!Input.GetKey("space")) spacePressed=false;
    3. if(Input.GetKey("space")  spacePressed==false){
    4. spacePressed=true;
    5. --- put your velocity stuff here to move upwards.
    6. }
    7.