Search Unity

Shoot At Player - logic help

Discussion in '2D' started by Crede-Pendrel, Aug 31, 2017.

  1. Crede-Pendrel

    Crede-Pendrel

    Joined:
    Jul 5, 2016
    Posts:
    23
    Hello Community

    I am working on a 2d platformer and following a tutorial by James Doyle (YouTube star: gamesplusjames). Particularly I am watching video 23 titled "How to Make Your Enemies Fire Projectiles" and having a problem understanding how the logic is supposed to work out.Lines 28-36 are where I need advice .If you need me to post more screenshots or code snippets just let me know and I will ASAP

    Original Video found here:


    Me trying to type out the logic....
    If the Enemy localScale is -1 && the players transform.X is GREATER then the enemy's transform.X && playerTransformPosition GREATER then the enemy transformX + player range.


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class ShootAtPlayerInRange : MonoBehaviour {
    7.  
    8.     public float playerRange;
    9.     public GameObject enemyProjectile;
    10.     private PlayerController thePlayer;
    11.     public Transform projectilePoint;
    12.  
    13.     public float waitBetweenShots;
    14.     private float shotCounter;
    15.  
    16.     // Use this for initialization
    17.     void Start () {
    18.         thePlayer = FindObjectOfType<PlayerController>();
    19.         shotCounter = waitBetweenShots;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update () {
    24.         Debug.DrawLine(new Vector3(transform.position.x - playerRange, transform.position.y, transform.position.z), new Vector3(transform.position.x + playerRange, transform.position.y, transform.position.z));
    25.  
    26.         shotCounter -= Time.deltaTime;    
    27.      
    28.         /[B]//movingleft left Shoots left
    29.         if (transform.localScale.x < 0 && thePlayer.transform.position.x > transform.position.x && thePlayer.transform.position.x < transform.position.x + playerRange && shotCounter > 0)
    30.         {[/B]
    31.             Instantiate(enemyProjectile, projectilePoint.position, projectilePoint.rotation);
    32.             shotCounter = waitBetweenShots;
    33.         }      [B]
    34.      
    35.         if (transform.localScale.x > 0 && thePlayer.transform.position.x < transform.position.x && thePlayer.transform.position.x > transform.position.x - playerRange && shotCounter < 0)
    36.         {[/B]
    37.             Instantiate(enemyProjectile, projectilePoint.position, projectilePoint.rotation);
    38.             shotCounter = waitBetweenShots;
    39.         }
    40.     }
    41. }
    42.  
    And the enemyFireballController script..........

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyFireballController : MonoBehaviour {
    6.  
    7.     public float speed;
    8.     public float rotationSpeed;
    9.     private PlayerController player;
    10.     private LevelManager theLevelManager;
    11.     public GameObject impactEffect;
    12.     public int damageToGive;
    13.  
    14.  
    15.     // Use this for initialization
    16.     void Start()
    17.     {
    18.         player = FindObjectOfType<PlayerController>();
    19.         theLevelManager = FindObjectOfType<LevelManager>();
    20.  
    21.  
    22.         if (player.transform.position.x < transform.position.x)
    23.         {
    24.             speed = -speed;
    25.             rotationSpeed = -rotationSpeed;
    26.         }
    27.     }
    28.  
    29.     // Update is called once per frame
    30.     void Update()
    31.     {
    32.         GetComponent<Rigidbody2D>().velocity = new Vector2(speed, GetComponent<Rigidbody2D>().velocity.y);
    33.         GetComponent<Rigidbody2D>().angularVelocity = rotationSpeed;
    34.     }
    35.  
    36.     private void OnTriggerEnter2D(Collider2D collision)
    37.     {
    38.         if (collision.tag == "Player")
    39.         {
    40.             theLevelManager.HurtPlayer(damageToGive);
    41.          
    42.         }
    43.  
    44.         Instantiate(impactEffect, transform.position, transform.rotation);
    45.         Destroy(gameObject);
    46.     }
    47. }
    48.  
    49.  
     
    Last edited: Aug 31, 2017