Search Unity

Change this C# script to work with Unity, instead of OpenGameEngine?

Discussion in 'Scripting' started by Jeezker, Apr 20, 2014.

  1. Jeezker

    Jeezker

    Joined:
    Sep 25, 2013
    Posts:
    51
  2. smitchell

    smitchell

    Joined:
    Mar 12, 2012
    Posts:
    702
    you really shouldn't ask people to write scripts for you but here's a basic version of that script.. you'll want to play around with the values.. and this version isn't the best way to do things in unity, you should look into rigid body's and applying force...

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class Chopper : MonoBehaviour {
    6.    
    7.     private float Acceleration = 0.4f;
    8.     private float MaxGravity = 1f;
    9.     private float MaxVelocity;
    10.     public bool Power;
    11.     private float Velocity;
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         MaxGravity = -0.1f;
    16.         MaxVelocity = 0.4f;
    17.         Power = false;
    18.         Velocity = -1f;
    19.     }
    20.    
    21.     // Update is called once per frame
    22.     void Update () {
    23.         if(Input.GetMouseButton(0)) {
    24.             Power = true;  
    25.         } else {
    26.             Power = false; 
    27.         }
    28.    
    29.         if(Power) {
    30.             Velocity += Acceleration * 0.6f;
    31.            
    32.             if(Velocity < MaxVelocity) {
    33.                 Velocity = MaxVelocity;
    34.             }
    35.                
    36.         } else {
    37.             Velocity -= Acceleration;
    38.  
    39.             if (Velocity > MaxGravity) {
    40.                 Velocity = MaxGravity;
    41.             }
    42.         }
    43.        
    44.         float newYPos = transform.position.y;
    45.         newYPos += Velocity;
    46.        
    47.         transform.position = new Vector3(transform.position.x, newYPos, 0);
    48.     }
    49. }
    50.