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
1
Question by Heke · Jun 08, 2013 at 09:01 PM · 2drotationquaternionmissilehoming

Homing missile not working.

Hi. Im trying to make a 2d spaceship game. Im making a homing missile, but when I fire it, it won't follow the enemies.

My enemies have tag "nem", and here's my missile code: #pragma strict

 var target : Transform;
 
 function Start () {
 
 }
 
 function Update () {
     if(target){
         Debug.Log("A");
         transform.LookAt(target);
     }
     transform.Translate(Vector3.up * Time.deltaTime * 10);
 }
 
 function OnCollisionEnter(other : Collision){
     if(other.gameObject.tag == ("nem")){
         target = other.gameObject.transform;
         Debug.Log("B");
     }
 }
 //
 //function OnCollisionEnter

If I use the on collision enter however, the missile goes crazy so I don't want to use it. What's the difference anyway? colliding or being inside object's trigger zone I guess. Oh and it won't show either A -debug.log nor B. Should be easy for you.

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Jun 08, 2013 at 11:22 PM

How do you expect to chase the enemy if the target variable isn't being set? Setting it in OnCollisionEnter is useless, since this event will only occur when the missile hits the enemy! A possible solution is to do a raycast when the missile is fired, and if an enemy is hit set target to its transform - like this:

 var missileSpeed: float = 10;
 var target: Transform;
 
 function Start(){
   // move the missile at missileSpeed velocity:
   rigidbody.velocity = transform.forward * missileSpeed;
   // check if an enemy is in the line of fire:
   var hit: RaycastHit;
   if (Physics.Raycast(transform.position, transform.forward, hit)){
     if (hit.gameObject.CompareTag("nem")){
       target = hit.transform; // if so, lock the missile on it
     }
   }
 }
 
 function Update(){
   if (target){ // if target locked, set velocity in its direction
     transform.LookAt(target);
     rigidbody.velocity = transform.forward * missileSpeed;
   }
 }

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 Heke · Jun 08, 2013 at 11:26 PM 0
Share

I forgot to tell that I made the sphere collider a huge so it kind of collides with it all the time, but.. I'll test this now.

avatar image Heke · Jun 09, 2013 at 12:04 AM 0
Share

Okay now it works.. somehow. I just need to figure out how to lock more than one missile to one target. Thanks for your time.

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

15 People are following this question.

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

Related Questions

Problem at 180 degrees when rotating slowly 1 Answer

Creating a homing missile on 2D, problem with transform.lookat() 1 Answer

Lock Rotation of Object between 2 points, which is looking at the direction the mouse is pointing 0 Answers

Homing missile and LookAt issue 1 Answer

Rotating an object to target on the Z axis 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