Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

shooting 2d arrays

Discussion in '2D' started by ghost123_1234, Apr 23, 2015.

  1. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
    hello

    I have this script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerControl : MonoBehaviour
    5. {
    6.     public Transform sightStart, sightEnd;
    7.  
    8.     public Animation idle;
    9.  
    10.     RaycastHit2D interacted;
    11.  
    12.     public float speed = 1.0f;
    13.  
    14.     float jumpTime, jumpDelay = .3f;
    15.     bool jumped;
    16.  
    17.     Animator anim;
    18.  
    19.     public bool animation = true;
    20.  
    21.     public bool spottedenemy = false;
    22.  
    23.     void Start()
    24.     {
    25.         anim = GetComponent<Animator>();
    26.     }
    27.  
    28.     void Update()
    29.     {
    30.         Movement(); //call the function every frame
    31.         RaycastStuff(); //call the function every frame
    32.     }
    33.  
    34.     void RaycastStuff()
    35.     {
    36.         Debug.DrawLine(sightStart.position, sightEnd.position, Color.green);
    37.         spottedenemy = Physics2D.Linecast(sightStart.position, sightEnd.position,1<< LayerMask.NameToLayer("Enemy"));
    38.     }
    39.  
    40.  
    41.     void Movement() //function that stores all the movement
    42.     {
    43.        
    44.  
    45.         if(animation == true && spottedenemy == false)
    46.         {
    47.             speed = 6;
    48.             anim.SetFloat("speed", Mathf.Abs (-10F));
    49.             transform.Translate(Vector3.right * speed * Time.deltaTime);
    50.             transform.eulerAngles = new Vector2(0, 0); //this sets the rotation of the gameobject
    51.         }
    52.         else if(spottedenemy == true)
    53.         {
    54.             animation = false;
    55.             speed = 0;
    56.             anim.SetFloat("speed", 0);
    57.  
    58.            
    59.         }
    60.         else if(spottedenemy == false)
    61.         {
    62.             animation = true;
    63.         }
    64.  
    65.      
    66.    
    67.  
    68.  
    69.     }
    70. }
    71.  
    But what i wanna do if
    if spottedenemy = true
    it needs to shoot bullets. You dont need to see the bullets but the enemy health that it is colliding with needs to go down.
    i think i need to do this with a 2d racyast and when it hit a sendmessage
    but how do i do that
    hope someone can help
     
  2. ghost123_1234

    ghost123_1234

    Joined:
    Jan 9, 2015
    Posts:
    88
  3. ChuanXin

    ChuanXin

    Unity Technologies

    Joined:
    Apr 7, 2015
    Posts:
    1,068
    You could retrieve the spotted game object after the line cast. With that game object, you could either spawn bullets which would hit the game object in the future, or straight up reduce the health of the spotted enemy.