Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

Homing Missile targetting the nearest Enemy

$
0
0
Hello, I made a script, with a Homing Missile in Unity2D. Game is a Sidescrolling Platformer Contrastyle. The missile, which is fired from a gun muzzle is doing it´s job. However.. It doesn´t recognize, which enemy is currently the nearest Target. I tried to google that. I also found an answer in the forums, but it is using Linq. Since I have absolutely no clue about Linq, is there another solution I could use? Currently I am using "FindGameObjectWithTag to find the target for the missile. I also know, that I somehow have to make it work that, so that the missile recognizes a certain distance to the nearest enemy, but I don´t get, how to properly set it up. Do I have to use an Array for that? Another solution could be, to make the missile just allow to search for a target, if it is visible on the screen? However, my recent trys with this were a desaster. Help or a hint would be much appreciated, since at the moment if I am direclty in front of the enemy the missile is flying on to the next target like 2 screens away. Thanks a lot. Here´s the script. public GameObject target; Rigidbody2D rb; // Use this for initialization void Start () { } // Update is called once per frame void FixedUpdate () { rb = GetComponent(); if(target = GameObject.FindGameObjectWithTag ("Enemy")) { Vector2 pointToTarget = (Vector2)transform.position-(Vector2)target.transform.position; pointToTarget.Normalize (); float value = Vector3.Cross(pointToTarget, transform.right).z; if(value >0) { rb.angularVelocity = rotatingSpeed; } else if (value < 0) rb.angularVelocity = -rotatingSpeed; else rb.angularVelocity = 0; } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>