• 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 /
  • Help Room /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Spaceman28 · May 25, 2017 at 11:07 PM · 2dfollow playermissilerockethoming

Homing Missiles!

Hello, I am currently attempting to create a game that is a top down spaceship game. This game is an android made app that is similar to the "Missiles!" application currently on the Google Play Store. I have made the actual player (The space ship), but I am having immense trouble with the homing missiles that follow and try to destroy the player. I want the missiles to be like the one in the aforementioned "Missiles!" game. But I cannot recreate them even though I have went through multiple threads and videos about this. I am really stuck right now, and help would be greatly appreciated... I want this to be a 2D missile which follows a 2D character object, on a 2D background. But as I said, the missiles don't currently work. Thank 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
0

Answer by Phantom_101 · Jun 29, 2017 at 06:14 PM

Here's the code I created for a missile.

 using UnityEngine;
 using System.Collections;
 
 public class Missile : MonoBehaviour {
     public float RangeTilShutdown;
     public int DamageToShields;
     public int DamageToHull;
     public GameObject ExplosionSmall;
     public Vector3 PositionSpawned;
     private GameObject[] Flares;
     private GameObject[] Hostiles;
     private GameObject MissileTarget;
     private bool Ready = false;
 
     void Start () {
         PositionSpawned = gameObject.transform.position;
         Flares = GameObject.FindGameObjectsWithTag ("Flare");
         Hostiles = GameObject.FindGameObjectsWithTag ("Hostile");
         if (Flares.Length > 0) {
             MissileTarget = Flares [0];
         } else if (Hostiles.Length > 0) {
             MissileTarget = Hostiles [0];
         }
     }
 
     void Update () {
         if (MissileTarget != null) {
             GetComponent<SlerpToLookAt> ().Target = MissileTarget.transform;
         } else {
             Destroy (gameObject);
         }
         if (Vector3.Distance (gameObject.transform.position, PositionSpawned) >= RangeTilShutdown) {
             Destroy (gameObject);
         }
     }
 
     void OnCollisionEnter(Collision Other) {
         ContactPoint Contact = Other.contacts [0];
         Quaternion Rot = Quaternion.FromToRotation (Vector3.up, Contact.normal);
         Vector3 Pos = Contact.point;
         Instantiate (ExplosionSmall, Pos, Rot);
         if (Other.gameObject.GetComponent<Shield> ().ComponentDisabled == false) {
             int TargetDamageVariance = Other.gameObject.GetComponent<Shield> ().DamageVariance;
             int DamageDealt = DamageToShields + Mathf.RoundToInt(Random.Range(-TargetDamageVariance, TargetDamageVariance));
             Other.gameObject.GetComponent<Shield> ().Power -= DamageDealt;
         } else if (Other.gameObject.GetComponent<HullIntegrity> ().ComponentDisabled == false) {
             int TargetDamageVariance = Other.gameObject.GetComponent<HullIntegrity> ().DamageVariance;
             int DamageDealt = DamageToHull + Mathf.RoundToInt (Random.Range (-TargetDamageVariance, TargetDamageVariance));
             Other.gameObject.GetComponent<HullIntegrity> ().Integrity -= DamageDealt;
         }
         Destroy (gameObject);
     }
 }
 

This is SlerpToLookAt.cs:

 using UnityEngine;
 using System.Collections;
 
 public class SlerpToLookAt: MonoBehaviour {
     public Transform Target;
     public float RotationSpeed;
 
     private Quaternion _LookRotation;
     private Vector3 _Direction;
 
     void Update () {
         _Direction = (Target.position - transform.position).normalized;
         _LookRotation = Quaternion.LookRotation (_Direction);
         gameObject.transform.rotation = Quaternion.Slerp (transform.rotation, _LookRotation, Time.deltaTime * RotationSpeed);
     }
 }

Be sure to also attach SlerpToLookAt.cs onto the missile. Also attach rigidbody and constant force. On rigidbody, uncheck "Use Gravity". On constant force, put the speed desired in the z axis of relative force. By the way, the flares gameobject[] is for antimissile flares. You can remove that if you want. I used this for a 3D game, but it probably can also work in 2D.

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

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

152 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 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 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 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 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 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 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 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 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 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 avatar image avatar image

Related Questions

How do I complete homing missile movement? 0 Answers

Is it possible to get a number of pre-set hashtags per minute from twitter? 0 Answers

Homing missile just rolls toward the player/not floating 2 Answers

SOLVED: How to properly follow in high velocity in 2D? 0 Answers

following round transparent around player (2D) 0 Answers


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