- Home /
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--;
}
}
Firstly, use FixedUpdate
in s$$anonymous$$d 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?
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?
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.
Answer by electricsauce · Aug 12, 2012 at 01:42 PM
Try lowering smooth. I think time.deltatime * smooth has to end up being less than 1.
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.
oh. how would I get it to move at a constant speed then?
Your answer
Follow this Question
Related Questions
Missilerotation problem with homing missile 0 Answers
missile homing script help 1 Answer
need help on creating this missile launching thing. 0 Answers
Fire missile to mouse click 1 Answer