• 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 FREAKINGREX · Apr 08, 2020 at 02:26 AM · arraysliststargetingtargetting

Array help with targeting system

I have a targeting system in my game where the player can press and input and then target an enemy if that enemy can be seen (via a separate script on the enemy with a raycast to the player). I have functionality to switch between the targets with an array and list set up in my target controller script. The issue I am having is that that raycast check is not working for the switching of enemy targets, just the initial targeting. Ill show my targeting script, I'm just confused and still learning how to code properly.

 public class TargetController : MonoBehaviour
 {
     Camera cam;
     EnemyInView target;
     Vector3 targetPos;
     Image crosshair;
     Vector3 crossHairPos;
     RayCastCheck[] rcc;
     bool playerIsHere;
     //public RayCastCheck rcc4;
 
     public static bool lockedOn;
     public static int lockedEnemy;
     public static int lockedEnemyPos;
 
     public static List<EnemyInView> nearByEnemies;
     public float Power = 10;
     private Vector3 scale = new Vector3(0.5f, 0.5f, 0.5f);
 
     public GameObject lockedSound;
     public GameObject changeSound;
     EnemyInView[] ev;
     PauseMenu pm;
 
     private void Awake()
     {
         nearByEnemies = new List<EnemyInView>();
         ev = FindObjectsOfType<EnemyInView>();
         rcc = FindObjectsOfType<RayCastCheck>();
         pm = FindObjectOfType<PauseMenu>();
     }
 
     private void Start()
     {
         cam = Camera.main;
         crosshair = gameObject.GetComponent<Image>();
         crossHairPos = new Vector3(crosshair.transform.position.x, crosshair.transform.position.y, crosshair.transform.position.z);
 
         lockedOn = false;
         lockedEnemy = 0;
     }
 
     private void Update()
     {
         nearByEnemies.RemoveAll(item => item == null);
 
         if (lockedEnemy >= 0 && lockedEnemy <= nearByEnemies.Count - 1)
         {      
             PlayerInput();
             LockOnChecks();
             RayCheck();
 
         }
         else
         {
             return;
         }
     }
 
     void PlayerInput()
     {
         if (Input.GetKeyDown(KeyCode.Z) && !lockedOn && playerIsHere)
         {
             if (nearByEnemies.Count >= 1)
             {
                 lockedOn = true;
                 crosshair.enabled = true;
 
                 lockedEnemy = 0;
                 target = nearByEnemies[lockedEnemy];
                 lockedSound.GetComponent<AudioSource>().Play();
             }
         }
 
         else if (Input.GetKeyDown(KeyCode.Z) && lockedOn|| nearByEnemies.Count == 0)
         {
             lockedOn = false;
             crosshair.enabled = false;
             lockedEnemy = 0;
             target = null;
             lockedSound.GetComponent<AudioSource>().Play();
         }
 
         if (Input.GetKeyDown(KeyCode.X) && playerIsHere)
         {
                 if (lockedEnemy == nearByEnemies.Count - 1)
                 {
                     lockedEnemy = 0;
                     target = nearByEnemies[lockedEnemy];
                     changeSound.GetComponent<AudioSource>().Play();
                 }
                 else
                 {
                     lockedEnemy++;
                     target = nearByEnemies[lockedEnemy];
                     changeSound.GetComponent<AudioSource>().Play();
                 }
         }
     }
 
     void LockOnChecks()
     {
         if (ev != null)
         {
             if (nearByEnemies[lockedEnemy].GetComponent<Renderer>().isVisible == false)
             {
                 lockedOn = false;
             }
 
             if (lockedOn)
             {
                 target = nearByEnemies[lockedEnemy];
 
                 gameObject.transform.position = cam.WorldToScreenPoint(target.transform.position);
                 if(pm != null)
                 {
                     if(pm.gameIsPaused == false)
                     {
                         gameObject.transform.Rotate(new Vector3(0, 0, -1));
                     }
                     else
                     {
                         gameObject.transform.Rotate(new Vector3(0, 0, 0));
                     }
                 }
                 else
                 {
                     return;
                 }
                 
 
                 Vector3 v3 = gameObject.transform.position = cam.WorldToScreenPoint(target.transform.position);
 
                 if (v3.z < 0)
                     v3 *= -1;
 
                 gameObject.transform.position = v3;
             }
 
             else if (!lockedOn || !playerIsHere)
             {
                 crosshair.enabled = true;
                 crosshair.transform.position = crossHairPos;
                 crosshair.transform.eulerAngles = new Vector3(0f, 0f, 0f);
             }
 
             if (nearByEnemies[lockedEnemy].GetComponent<Renderer>().isVisible == false)
             {
                 lockedOn = false;
             }
         }
         else
         {
             return;
         }
     }
 
     void RayCheck()
     {
         for(int i = 0; i < rcc.Length; ++i)
         {
             if(rcc[i].playerInRoom == true)
             {
                 playerIsHere = true;
             }
             else if(rcc[i].playerInRoom == false)
             {
                 playerIsHere = false ;
             }
         }
     }
 }
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

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.

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

199 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

Related Questions

snake type game (dynamic array / list) 0 Answers

How can you find the index or a variable associated to said index, from a script run from that instance of the game object? 0 Answers

Getting the element that has the same element number on another list ? 2 Answers

Copying a list of lists of int's in c# 1 Answer

I have trouble understanding arrays and enums. When and how? 0 Answers

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