how to add gravity to this script

var target : Transform; //the enemy's target
var moveSpeed = 5; //move speed
var rotationSpeed = 3; //speed of turning
var gravity:float = 20.0;
var Lives = 10;
private var dead = false;

private var moveDirection:Vector3 = Vector3.zero;

var myTransform : Transform; //current transform data of this enemy

function OnMouseDown()
{
    if(OnMouseDown)
    {
     Dead = true;
     Lives -= 1;
    }
print(Lives);
}

function Awake()
{
    myTransform = transform; //cache transform data for easy access/preformance
}

function Start()
{
     target = GameObject.FindWithTag("Player").transform; //target the player

}

function Update () 
{
    //rotate to look at the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

    //Apply gravity
    moveDirection.y -= gravity * Time.deltaTime;

    switch(Lives)
    {
         case 5:
         moveSpeed = 3;
         break;

         case 0:
         Destroy(gameObject);
         break;
    }

}

hello guys i need help adding gravity to this script and i want to no how to do it aswell if you can help me thanks

It looks like there is code in there to handle gravity. The problem is, it's adding a constant velocity to the y-movement of the object each frame. So it won't look right.

Real gravity is a force that produces acceleration- the velocity itself increases each second (until a maximum is reached due to wind resistance, or something else pushes back, like the ground.)

1) Easiest solution is to set the gravity variable to 0 (to turn it off.) Add a rigidbody to your object instead. By default its "use gravity" setting will be on. Just press play and watch it fall. This also means that it can hit other things like the ground, and Unity will handle it all for you.

Or, turn off that checkbox and instead of translating your object, you can apply forces. There is code here: http://answers.unity3d.com/questions/17341/changing-gravity-for-one-object Note that it's using the object's rigidbody mass and Unity's gravity constant, which are set by you in the editor, but you could plug in your own numbers there instead if you wanted.

2) If you can't use rigidbodies and forces for some reason, you'll have to add a new variable to track the current velocity of the object yourself.

Let's say we add a float gVelocity and set it to 0 in Start(). Then in Update(), change

//Apply gravity
moveDirection.y -= gravity * Time.deltaTime;

to

//Apply gravity
gVelocity += gravity * Time.deltaTime;
moveDirection.y -= gVelocity;

(In this case I assume gVelocity is a float, to keep it simple. A more elaborate solution would be to store the velocity as a vector, then velocity wouldn't just be downwards.)

so is this right

var target : Transform; //the enemy's target
var moveSpeed = 5; //move speed
var rotationSpeed = 3; //speed of turning
var gravity:float = 20.0;
var Lives = 10;
private var dead = false;

private var moveDirection:Vector3 = Vector3.zero;

var myTransform : Transform; //current transform data of this enemy

function OnMouseDown()
{
    if(OnMouseDown)
    {
     Dead = true;
     Lives -= 1;
    }
print(Lives);
}

function Awake()
{
    myTransform = transform; //cache transform data for easy access/preformance
}

function Start()
{
    var gVelocity:float = 0;

    target = GameObject.FindWithTag("Player").transform; //target the player
}

function Update () 
{
    //rotate to look at the player
    myTransform.rotation = Quaternion.Slerp(myTransform.rotation,
    Quaternion.LookRotation(target.position - myTransform.position), rotationSpeed*Time.deltaTime);

    //move towards the player
    myTransform.position += myTransform.forward * moveSpeed * Time.deltaTime;

    //Apply gravity
    gVelocity += gravity * Time.deltaTime;
    moveDirection.y -= gVelocity;

    switch(Lives)
    {
         case 5:
         moveSpeed = 3;
         break;

         case 0:
         Destroy(gameObject);
         break;
    }

}

sorry im just a beginner