x


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

more ▼

asked Sep 02 '10 at 09:49 AM

josif gravatar image

josif
212 19 23 35

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.)

more ▼

answered Sep 02 '10 at 10:56 AM

Bampf gravatar image

Bampf
5k 8 19 49

(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Sep 03 '10 at 11:59 PM

josif gravatar image

josif
212 19 23 35

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3325
x957
x651
x459
x47

asked: Sep 02 '10 at 09:49 AM

Seen: 4980 times

Last Updated: Sep 02 '10 at 09:49 AM