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

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

List resulting in out of range 0 Answers

Spawn unique objects from the array 1 Answer

Targeting Script Errors 0 Answers

Switch statement nested in a for loop runs only in one iteration of the loop 1 Answer

snake type game (dynamic array / list) 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