Search Unity

3D Platfomer Targeting Script HELP

Discussion in 'Immediate Mode GUI (IMGUI)' started by Jayland, Jun 24, 2012.

  1. Jayland

    Jayland

    Joined:
    Jun 23, 2012
    Posts:
    9
    I absolutely cannot understand this for the life of me. The pourpose of this script is when the player is near an enemy with the tag "Enemy", AND the player is holding down "Fire2", AND the enemy is on screen, the musket and head will be able to look at the enemy, and a crosshair is supposed to stay over the enemy it's near, but that's not the case here. I don't know what to put in the places of the code that has *enemy here*. Do I have to FindGameObjectsWithTag and do something like that? I believe I tried that and it didn't work. If anyone can help me, that be appreciated. NOTE: Every enemy in the world is the same, same tag, same AI. I'm really copying and paste the enemies everywhere once I drag a single prefab out.

    Code (csharp):
    1.  
    2. var musket : Transform;
    3. var crosshair : Transform;
    4. var head : Transform;
    5. var distance = 20.0;
    6.  
    7.  
    8. function Update(){
    9.         if(Vector3.Distance(*enemy here*.position, transform.position < distance  Input.GetButtonDown("Fire2")  *enemy here*.renderer.isVisible){
    10.             musket.transform.LookAt(*enemy here*);
    11.             head.transform.LookAt(*enemy here*);
    12.             crosshair.guiTexture.enabled = true;
    13.             crosshair.transform.position = Camera.main.WorldToViewportPoint(*enemy here*);     
    14.         else{
    15.             musket.transform.localRotation = Quaternion.identity;
    16.             head.transform.localRotation = Quaternion.identity;
    17.             crosshair.guiTexture.enabled = false;          
    18.         }
    19. }
    20.  
     
  2. Finjitzu

    Finjitzu

    Joined:
    Sep 8, 2011
    Posts:
    160
    Yes you need to find all the enemies first then loop through them. Something like this should work.

    Code (csharp):
    1.  
    2.  
    3. var EnemyArray : GameObject[];
    4.  
    5. function Start(){
    6.  
    7.        EnemyArray = GameObject.FindGameObjectsWithTag("Enemy");
    8.  
    9. }
    10.  
    11.  
    Then you'll have to loop through EnemyArray. Add this in your Update function or call a coroutine.

    Code (csharp):
    1.  
    2.  
    3. for(var i = 0; i < EnemyArray.length(); i++){
    4.  
    5.     if(Vector3.Distance(EnemyArray[i].transform.position, transform.position < distance  Input.GetButtonDown("Fire2") &EnemyArray[i].transform.renderer.isVisible){
    6.  
    7.             musket.transform.LookAt(EnemyArray[i].transform);
    8.  
    9.             head.transform.LookAt(EnemyArray[i].transform);
    10.  
    11.             crosshair.guiTexture.enabled = true;
    12.  
    13.             crosshair.transform.position = Camera.main.WorldToViewportPoint(EnemyArray[i].transform);    
    14.  
    15.         else{
    16.  
    17.             musket.transform.localRotation = Quaternion.identity;
    18.  
    19.             head.transform.localRotation = Quaternion.identity;
    20.  
    21.             crosshair.guiTexture.enabled = false;          
    22.  
    23.         }
    24. }
    25.  
    26.  
    Not perfect, but should get you on the right path.
     
    Last edited: Jun 25, 2012
  3. Jayland

    Jayland

    Joined:
    Jun 23, 2012
    Posts:
    9
    It took care of the past problems, but now it brought up some new ones I don't understand:
    1) (13,34): BCE0077: It is not possible to invoke an expression of type 'int'.
    2)(14,85): BCE0051: Operator '<' cannot be used with a left hand side of type 'UnityEngine.Vector3' and a right hand side of type 'float'.
    3)(18,88): BCE0017: The best overload for the method 'UnityEngine.Camera.WorldToViewportPoint(UnityEngine.Vector3)' is not compatible with the argument list '(UnityEngine.Transform)'.
    Code (csharp):
    1.  
    2. var enemy : GameObject[];
    3. var musket : Transform;
    4. var crosshair : Transform;
    5. var head : Transform;
    6. var distance = 20.0;
    7.  
    8. function Start(){
    9.     enemy = GameObject.FindGameObjectsWithTag("Enemy");
    10. }
    11.  
    12.  
    13. function Update(){
    14.     for(var i = 0; i < enemy.length(); i++){
    15.         if(Vector3.Distance(enemy[i].transform.position, transform.position < distance  Input.GetButtonDown("Fire2")  enemy[i].transform.renderer.isVisible)){
    16.             musket.transform.LookAt(enemy[i].transform);
    17.             head.transform.LookAt(enemy[i].transform);
    18.             crosshair.guiTexture.enabled = true;
    19.             crosshair.transform.position = Camera.main.WorldToViewportPoint(enemy[i].transform);  
    20.         }
    21.         else{
    22.             musket.transform.localRotation = Quaternion.identity;
    23.             head.transform.localRotation = Quaternion.identity;
    24.             crosshair.guiTexture.enabled = false;
    25.         }
    26.     }
    27. }
    28.  
     
  4. Finjitzu

    Finjitzu

    Joined:
    Sep 8, 2011
    Posts:
    160
    Code (csharp):
    1.  
    2. if(Vector3.Distance(enemy[i].transform.position, transform.position < distance ......
    3.  
    should be

    Code (csharp):
    1.  
    2. if(Vector3.Distance(enemy[i].transform.position, transform.position) < distance ......
    3.  
    also

    Code (csharp):
    1.  
    2. crosshair.transform.position = Camera.main.WorldToViewportPoint(enemy[i].transform);
    3.  
    should be

    Code (csharp):
    1.  
    2. crosshair.transform.position = Camera.main.WorldToViewportPoint(enemy[i].transform.position);
    3.  
    Although I'm not too sure about WorldToViewPoint reference might need some more reading...

    You might get more reply's if you posted in the scripting forum.
     
    Last edited: Jun 26, 2012
  5. Jayland

    Jayland

    Joined:
    Jun 23, 2012
    Posts:
    9
    I only got one reply, and it's the same as you put. All I had a problem with was the (enemy.transform.position), and some of my variables were put in from the project prefabs, and not the objects in the hiearchy.

    So now, it's just about complete. All that needs to be done is to reset the player to his regular state when he destroys an enemy. So I tried this:

    Code (csharp):
    1.  
    2.         if(jesus.transform == null){
    3.             crosshair.guiTexture.enabled = false;
    4.             musket.transform.localRotation = Quaternion.identity;
    5.             head.transform.localRotation = Quaternion.identity;
    6.         }
    7.  
    Didn't work...but I'll see if I can find something.

    EDIT: This script works, but not for multiple enemies. It only targets the first copied enemy, then it'll target the next copied enemy. Perhaps if it was more like "target the enemy that meets the conditions AND which one is closest to the player."
     
    Last edited: Jun 26, 2012
  6. Finjitzu

    Finjitzu

    Joined:
    Sep 8, 2011
    Posts:
    160
    Lol you're main character is jesus? Or are you shooting jesus? Either way that's pretty awesome :)
     
  7. Jayland

    Jayland

    Joined:
    Jun 23, 2012
    Posts:
    9
    lol. naw, I just put in a random variable. I couldn't think of anything but jesus. I love jesus btw lol:)