x


help with using lerp?

hey i'm making a homing missile script and I want it to constantly track the movement of the target. here's my code. so far what it does is drop off the pylon and then speed up forwards. I don't think I'm using lerp right in this script, since it starts the object off just sitting in front of the target and then jumping back and falling from the original location. where and how should I use lerp?

 #pragma strict
var projectile1 : GameObject; //the actual GOs of the missiles, whose rigidbodies are used for
var projectile2 : GameObject; //instantiatedprojectiles, and then destroyed once launched.
var projectile3 : GameObject;
var projectile4 : GameObject;

var Pod1 : Transform; //Pods, used for the start location of instantiatedprojectiles
var Pod2 : Transform;
var Pod3 : Transform;
var Pod4 : Transform;

private var m1 : boolean = true; //booleans used for the sequencing of missile launches
private var m2 : boolean = false;
private var m3 : boolean = false;
private var m4 : boolean = false;

private var m1seek : boolean = false; //booleans used for the seeking action of missiles
private var m2seek : boolean = false;
private var m3seek : boolean = false;
private var m4seek : boolean = false;

private var instantiatedProjectile1 : Rigidbody; //instantiated missile clones
private var instantiatedProjectile2 : Rigidbody;
private var instantiatedProjectile3 : Rigidbody;
private var instantiatedProjectile4 : Rigidbody;

var target : Transform; //miscellaneous variables
var smooth = 1.0;
var reloadTime = 0.5;
var ammoCount = 8;
var lastShot = -10.0;


function Update () {

//stopping the y-motion effect that gravity creates
//m1
 if (instantiatedProjectile1.rigidbody.velocity.z > 10)
 {
     instantiatedProjectile1.rigidbody.velocity.y = 0;
        m1seek = true;
 }

//m2
 if (instantiatedProjectile2.rigidbody.velocity.z > 10)
 {
     instantiatedProjectile2.rigidbody.velocity.y = 0;
        m2seek = true;
 }

//m3
 if (instantiatedProjectile3.rigidbody.velocity.z > 10)
 {
     instantiatedProjectile3.rigidbody.velocity.y = 0;
        m3seek = true;
 }

//m4
 if (instantiatedProjectile4.rigidbody.velocity.z > 10)
 {
     instantiatedProjectile4.rigidbody.velocity.y = 0;
        m4seek = true;
 }

//capping the max constantforce so that the missile doesn't infinitely speed up
//m1
 if (instantiatedProjectile1.rigidbody.velocity.z > 20) 
 {
 instantiatedProjectile1.rigidbody.constantForce.force = (Vector3.forward*0);
 } 

//m2
 if (instantiatedProjectile2.rigidbody.velocity.z > 20) 
 {
 instantiatedProjectile2.rigidbody.constantForce.force = (Vector3.forward*0);
 } 

//m3
 if (instantiatedProjectile3.rigidbody.velocity.z > 20) 
 {
 instantiatedProjectile3.rigidbody.constantForce.force = (Vector3.forward*0);
 }

//m4
 if (instantiatedProjectile4.rigidbody.velocity.z > 20) 
 {
 instantiatedProjectile4.rigidbody.constantForce.force = (Vector3.forward*0);
 }
}

function FixedUpdate () {

//missile tracking processes
//m1
 if (m1seek)
 {
 instantiatedProjectile1.transform.position = Vector3.Lerp (instantiatedProjectile1.transform.position, target.position,Time.deltaTime * smooth);
 }

//m2
 if (m2seek)
 {
 instantiatedProjectile2.transform.position = Vector3.Lerp (instantiatedProjectile2.transform.position, target.position,Time.deltaTime * smooth);
 }

//m3
 if (m3seek)
 {
 instantiatedProjectile3.transform.position = Vector3.Lerp (instantiatedProjectile3.transform.position, target.position,Time.deltaTime * smooth);
 }

//m4
 if (m4seek)
 {
 instantiatedProjectile4.transform.position = Vector3.Lerp (instantiatedProjectile4.transform.position, target.position,Time.deltaTime * smooth);
 }
}



function Fire () 
{

 if (Time.time > reloadTime + lastShot && ammoCount > 0) 
 {

 //creating a firing sequence, instantiating clones, and destroying GOs/adding gravity for realism
  if (m1)
        {   
            instantiatedProjectile1 = Instantiate (projectile1.rigidbody, Pod1.position, transform.rotation);
            Destroy (gameObject.FindWithTag("Missile 6"));
            instantiatedProjectile1.rigidbody.useGravity = true;
            yield WaitForSeconds(0.5);
            instantiatedProjectile1.rigidbody.useGravity = false;
            instantiatedProjectile1.rigidbody.constantForce.force = (Vector3.forward*10);
            yield WaitForSeconds(reloadTime);
            m2 = true;
        }    

 if (m2)
 {

 instantiatedProjectile2 = Instantiate (projectile2.rigidbody, Pod2.position, transform.rotation);
            Destroy (gameObject.FindWithTag("Missile 5"));
            instantiatedProjectile2.rigidbody.useGravity = true;
            yield WaitForSeconds(0.5);
            instantiatedProjectile2.rigidbody.useGravity = false;
            instantiatedProjectile2.rigidbody.constantForce.force = (Vector3.forward*10);
            yield WaitForSeconds(reloadTime);
            m3 = true;

 }

 if (m3)
 {
 instantiatedProjectile3 = Instantiate (projectile3.rigidbody, Pod3.position, transform.rotation);
            Destroy (gameObject.FindWithTag("Missile 8"));
            instantiatedProjectile3.rigidbody.useGravity = true;
            yield WaitForSeconds(0.5);
            instantiatedProjectile3.rigidbody.useGravity = false;
            instantiatedProjectile3.rigidbody.constantForce.force = (Vector3.forward*10);
            yield WaitForSeconds(reloadTime);
            m4 = true;
 }

 if (m4)
 {
 instantiatedProjectile4 = Instantiate (projectile4.rigidbody, Pod4.position, transform.rotation);
            Destroy (gameObject.FindWithTag("Missile 7"));
            instantiatedProjectile4.rigidbody.useGravity = true;
            yield WaitForSeconds(0.5);
            instantiatedProjectile4.rigidbody.useGravity = false;
            instantiatedProjectile4.rigidbody.constantForce.force = (Vector3.forward*10);
            yield WaitForSeconds(reloadTime);

 }

 Physics.IgnoreCollision(instantiatedProjectile1.collider, transform.root.collider);
 Physics.IgnoreCollision(instantiatedProjectile2.collider, transform.root.collider);
 Physics.IgnoreCollision(instantiatedProjectile3.collider, transform.root.collider);
 Physics.IgnoreCollision(instantiatedProjectile4.collider, transform.root.collider); 
 lastShot = Time.time;
 ammoCount--;
 }
}
more ▼

asked Aug 12 '12 at 07:31 AM

herp johnson gravatar image

herp johnson
66 7 12 13

Firstly, use FixedUpdate in stead of Update. If you're doing anything with rigidbodies or transforms in an update, you almost always want to use FixedUpdate, as it controls the rate of movement a lot more smoothly.

However, I don't really see what you're doing wrong... I've used lerp, myself, and as far as I can tell, you're using it right :P

Did you assign "target" in the inspector?

Aug 12 '12 at 01:18 PM DESTRUKTORR

Yes, I did. It's just a simple cube GO so that I can test it out. Would it help if I gave you the project?

Aug 13 '12 at 11:05 PM herp johnson

Ok so I changed a ton of my script, and now the missiles start in the right place and drop. However, I can't seem to make them shoot one at a time--they launch sequentially one after another. Then, they fly straight towards the target at a very high speed, and don't rotate towards it. How can I use lerp to slow them down and make them point towards the target?

Edit: I updated the code to my current one.

Aug 14 '12 at 03:01 AM herp johnson
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

Try lowering smooth. I think time.deltatime * smooth has to end up being less than 1.

more ▼

answered Aug 12 '12 at 01:42 PM

electricsauce gravatar image

electricsauce
532 19 26 34

It doesn't work that way... time.deltaTime is the time it took between the last frame and the new one, so multiplying it by a number larger than 1 makes it take longer. It's a common technique, really. The way it is now, it should take 5 seconds to get to its target, regardless of distance.

Aug 12 '12 at 02:18 PM DESTRUKTORR

oh. how would I get it to move at a constant speed then?

Aug 13 '12 at 11:05 PM herp johnson
(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:

x437
x58
x25

asked: Aug 12 '12 at 07:31 AM

Seen: 835 times

Last Updated: Aug 14 '12 at 03:05 AM