Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by herp johnson · Aug 12, 2012 at 07:31 AM · lerpmissilehoming

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--;
  }
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DESTRUKTORR · Aug 12, 2012 at 01:18 PM 1
Share

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?

avatar image herp johnson · Aug 13, 2012 at 11:05 PM 0
Share

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?

avatar image herp johnson · Aug 14, 2012 at 03:01 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DESTRUKTORR · Aug 12, 2012 at 02:18 PM 0
Share

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.

avatar image herp johnson · Aug 13, 2012 at 11:05 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

C# - 2D - How can I make my homing rockets follow my player, and when my player dies, the rocket just keeps going forward? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges