Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by WENKO · Feb 23, 2020 at 05:38 AM · shootingmovement scriptspace shootermissilehoming

How do I complete homing missile movement?

Dear everyone

I'm making shooting game. The Homing missile will do like this

  1. GetComponet or FindGameObjectWithTag for Players, Enemies

  2. Set the coordinate them

  3. Check distances between Player and Enemies.

  4. Let the shortest distance fire. 4-1. if the Enemy is there ---> Take it. 4-2. if the Enemy is disappeared ---> Just go straight

Here 4-2, when I push fire button several times and the Targeting Enemy is taken by first missile, and the next other Homing missiles have just stopped!!

I want to solve the 4-2 section over list.

That is, the problem on b under Figures.

a. Several times missiles are pushed alt text b. After Enemy destroyed, then other left missiles just there!! ---> Help me, please!! alt text

plus I attached the code.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerHMissile_2 : MonoBehaviour 
 {    
     float rotSpeed = 180f;
 
     Vector3 dir = new Vector3();
 
     public float missileSpeed;
 
     private    GameObject hazardBeingMeasured;
 
     public Vector3 measuredDistance;
 
     private GameObject closestTarget;
 
     private Vector3 distanceDifference;
     
     private Vector3 playerPosition;
 
     private float currentDistance;
     
     private float oldDistance;
 
     private GameObject[] hazardArray;
 
     void Start()
     {
 
         oldDistance = Mathf.Infinity;
 
         playerPosition = GameObject.FindGameObjectWithTag("Player-1").transform.position;
 
         hazardArray = GameObject.FindGameObjectsWithTag("Enemy");
 
         for (int i = 0; i < hazardArray.Length; i++) 
         {
             distanceDifference = hazardArray [1].transform.position - playerPosition;
 
             currentDistance = distanceDifference.sqrMagnitude;
             
              
             
             hazardBeingMeasured = hazardArray [1].gameObject;
 
             measuredDistance = hazardBeingMeasured.transform.position - playerPosition;
                                              
 
             if (currentDistance < oldDistance && hazardArray [i].GetComponent<Istargeted> ().isTargeted == false && playerPosition.z < hazardArray [i].transform.position.z - 5)
 
             if (currentDistance < oldDistance)
             {
                 closestTarget = hazardArray [i];
 
                 oldDistance = currentDistance;
         }
     }
 
   // Update is called once per frame
     void FixedUpdate ()
     {
         if (closestTarget != null) 
         {
             dir = closestTarget.transform.position - this.transform.position;
             dir.Normalize ();                                                                                          
 
             float zAngle = Mathf.Atan2 (dir.y, dir.x) * Mathf.Rad2Deg - 90;                                            
 
             Quaternion desiredRot = Quaternion.Euler (0, 0, zAngle);                                                   
 
             transform.rotation = Quaternion.RotateTowards (transform.rotation, desiredRot, rotSpeed * Time.deltaTime); 
 
             Vector3 pos = transform.position;                                                                          
 
             Vector3 velocity = new Vector3 (0, missileSpeed * Time.deltaTime, 0);                                      
             pos += transform.rotation * velocity;                                                                      
 
             transform.position = pos;                                                                                 
         }  
 
         if (closestTarget == null) 
         {
             Vector3 pos = transform.position;                                                                          
             Vector3 velocity = Vector3.forward * missileSpeed;                     
 
             pos += 1 * velocity;                                                                      
 
             transform.position = pos;
         }
         
     }
   
 }


1.jpg (21.8 kB)
2.jpg (31.5 kB)
Comment
Add comment · Show 2
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 WENKO · Feb 23, 2020 at 06:36 AM 0
Share

if (closestTarget != null)

if (closestTarget == null)

So, this way, Isn't it enough?

I want to see more another ways, please.

avatar image mdpapplications · Feb 29, 2020 at 08:44 AM 0
Share

How do you destroy the target?

0 Replies

· Add your reply
  • Sort: 

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

203 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 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

Won't fire when switching weapons? 0 Answers

[Unity, beginner. Help me.]Fire five parabolic lines in the desired random direction. 1 Answer

Space shooter - why after clicking play the bolt is shooting 0 Answers

Homing Missiles! 1 Answer

2D spaceship style shooting 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