Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Trying to have enemy fire at player .. problem is he only shoots to the right.

Discussion in 'Scripting' started by kdawgfsho04, Nov 28, 2020.

  1. kdawgfsho04

    kdawgfsho04

    Joined:
    Aug 3, 2020
    Posts:
    18
    Need help getting enemy to shoot in direction of player. here is the code of what i have.
    Code (CSharp):
    1.  public float speed;
    2.     private Transform player;
    3.     public float lineOfSite;
    4.     public float shootingRange;
    5.     public GameObject Bullet;
    6.     public Transform raycastPoint;
    7.     public float lastAttackTime;
    8.     public float attackDelay;
    9.     public float shootingForce;
    10.     private SpriteRenderer spriteRenderer;
    11.     private bool facingRight;
    12.     private bool facingDown;
    13.     private Rigidbody2D rb;
    14.     private Vector2 move;
    15.  
    16.  
    17.  
    18.  
    19.     void Start()
    20.     {
    21.         player = GameObject.FindGameObjectWithTag("Player").transform;
    22.         this.spriteRenderer = this.GetComponent<SpriteRenderer>();
    23.         rb = GetComponent<Rigidbody2D>();
    24.         facingRight = true;
    25.  
    26.  
    27.     }
    28.     void Update()
    29.     {
    30.  
    31.         this.spriteRenderer.flipX = player.transform.position.x < this.transform.position.x;
    32.         float distanceFromPlayer = Vector2.Distance(player.position, transform.position);
    33.         if (distanceFromPlayer < lineOfSite && distanceFromPlayer > shootingRange)
    34.         {
    35.             transform.position = Vector2.MoveTowards(this.transform.position, player.position, speed * Time.deltaTime);
    36.         }
    37.         else if (distanceFromPlayer <= shootingRange)
    38.         {
    39.             if (Time.time > lastAttackTime + attackDelay)
    40.             {
    41.                 if (!facingRight).... i tried player.transform.position.x < 0.. but that didnt work to well anytime the numbers were neg he always shot left
    42.                 {
    43.                  
    44.                     GameObject effect = Instantiate(Bullet, raycastPoint.position, raycastPoint.rotation);
    45.                     effect.GetComponent<Rigidbody2D>().AddRelativeForce(-transform.right * shootingForce, ForceMode2D.Force);
    46.                     lastAttackTime = Time.time;
    47.                 }
    48.                 else
    49.                 {
    50.                  
    51.                     GameObject effect = Instantiate(Bullet, raycastPoint.position, raycastPoint.rotation);
    52.                     effect.GetComponent<Rigidbody2D>().AddRelativeForce(transform.right * shootingForce, ForceMode2D.Force);
    53.                     lastAttackTime = Time.time;
    54.  
    55.                 }
    56.              
    57.  
    58.             }
    59.  
    60.         }
    61.     }
    62.  
    63.      private void OnDrawGizmos()
    64.      {
    65.          Gizmos.color = Color.green;
    66.          Gizmos.DrawWireSphere(transform.position, lineOfSite);
    67.          Gizmos.DrawWireSphere(transform.position, shootingRange);  
    68.      }
    69.        
     
  2. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    81
    It could be possible that you are not working on 2d mode. And x is not changing at all. Instead player's Y and Z axis are changing ? Can you check that once?
     
  3. kdawgfsho04

    kdawgfsho04

    Joined:
    Aug 3, 2020
    Posts:
    18
    oh im on 2d mode
     
  4. kdawgfsho04

    kdawgfsho04

    Joined:
    Aug 3, 2020
    Posts:
    18
    tried this... my enemy seems to kind of glitch around when he moves... anyway to fix that?

    Code (CSharp):
    1. }
    2.     void Update()
    3.     {
    4.  
    5.         this.spriteRenderer.flipX = player.transform.position.x < this.transform.position.x;
    6.         float distanceFromPlayer = Vector2.Distance(player.position, transform.position);
    7.         if (distanceFromPlayer < lineOfSite && distanceFromPlayer > shootingRange && isMoving)
    8.         {
    9.             transform.position = Vector2.MoveTowards(this.transform.position, player.position, speed * Time.deltaTime);
    10.             isMoving = true;
    11.         }
    12.         else if (distanceFromPlayer <= shootingRange && !isMoving)
    13.         {
    14.             isMoving = false;
    15.             if (Time.time > lastAttackTime + attackDelay)
    16.             {
    17.  
    18.                 transform.position = (player.transform.position - this.transform.position).normalized;
    19.                 GameObject effect = Instantiate(Bullet, raycastPoint.position, raycastPoint.rotation);
    20.                 effect.GetComponent<Rigidbody2D>().AddRelativeForce(transform.position * shootingForce, ForceMode2D.Impulse);
    21.                 lastAttackTime = Time.time;
    22.  
    23.  
    24.                
    25.  
    26.             }
    27.  
    28.         }