• 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 galaboy · Nov 12, 2013 at 07:13 AM · c#ai

homing missile how to start

am trying to do a homing missile effect in my work. need to know if there is any tutorials or intro to how to start with. need help.

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

Answer by Tomer-Barkan · Nov 12, 2013 at 07:32 AM

You can use my code, for a 2d game, although it should work for a 3d game as well I think:

 using UnityEngine;
 using System.Collections;
 
 public interface Damagable {
     int GetOwner();
     void TakeDamage(float damage);
 }
 
 public class Missile : MonoBehavoiur {
     private Transform target;
     private int owner;
 
     private float damage;
     protected float range;
     protected float rotationRate;
 
     public void CreateObject(Vector3 start, Transform target, Vector3 facing, int owner, float damage, float range, float speed, float rotationRate) {
         Missile missile = (Missile)Instantiate(this, start, Quaternion.identity);
         missile.owner = owner;
         missile.damage = damage;
         missile.range = range;
         missile.rotationRate = rotationRate;
         missile.rigidbody.velocity = facing.normalized * speed;
         missile.target = target;
     }
 
     public void FixedUpdate() {
         range -= rigidbody.velocity.magnitude * Time.fixedDeltaTime;
         if (range <= 0) {
             GameObject.Destroy(gameObject);
         }
 
         if (target == null)
             return;
 
         Vector3 toTarget = target.position - transform.position;
         Vector3 newVelocity = Vector3.RotateTowards(rigidbody.velocity, toTarget, rotationRate * Mathf.Deg2Rad * Time.fixedDeltaTime, 0); 
         newVelocity.z = 0;
 
         rigidbody.velocity = newVelocity;
     }
 
     public void OnTriggerEnter(Collider other) {
         MonoBehaviour[] monos = other.GetComponents<MonoBehaviour>();
         foreach (MonoBehaviour mono in monos) {
             if (typeof(Damagable).IsAssignableFrom(mono.GetType())) {
                 Damagable damaged = (Damagable)mono;
                 if (damaged.GetOwner() >= 0 && damaged.GetOwner() != owner) {
                     damaged.TakeDamage(damage);
                     GameObject.Destroy(this.gameObject);
                 }
             }
         }
     }
 }

Now, every object that you want the missile to be able to hit must implement the Damagable interface, and must have a different owner than the missile (that way missiles don't hit friendly targets). You can remove the owner check if you want friendly fire to be possible.

To launch the missile, create a missile prefab, attach this script, and when you want to launch it, call the CreateObject() method of the prefab.

Note that this was written for a torpedo style missile, so it's more like a ball of energy. That's why the mesh doesn't rotate. If you have a missile that has a head and tail, you'll want to rotate it in the FixedUpdate() to face the direction of the velocity:

 transform.forward = rigidbody.velocity.normalized;
Comment
Add comment · Show 4 · 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 galaboy · Nov 12, 2013 at 07:53 AM 0
Share

thanks for ur help, but am new to scripting and unity3d, just give me a start how to do or tell me where i can learn this stuff, i'll do it by myself, so that i can can learn. please don't take this on the wrong side.

avatar image Tomer-Barkan · Nov 12, 2013 at 07:56 AM 0
Share

You can learn basic scripting from unity's tutorials:

http://unity3d.com/learn/tutorials/modules/beginner/scripting

avatar image galaboy · Nov 12, 2013 at 10:50 AM 0
Share

doing a small 2d drone game as my demo reel, where my player has to have a set of weapon system. puzzled about what to add. doing a missile is a bit advanced, if u can, suggest me.

avatar image Tomer-Barkan · Nov 12, 2013 at 11:00 AM 0
Share

Projectiles are a good start. Laser is a bit more complicated (visually displaying the laser is). $$anonymous$$issile is not so bad, it's just a projectile that changes direction to follow a target. Those are the 3 main weapon types I can think of.

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

17 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

Related Questions

A* Pathfinding Help please 1 Answer

Multiple Cars not working 1 Answer

Movement around a huge object by avoiding obstacles 1 Answer

Confused about copying Lists! 1 Answer

Distribute terrain in zones 3 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges