Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 WhatIsInAName · Jan 05, 2021 at 10:00 PM · raycast2d-platformershootingenemy aiinstantiate prefab

Patrolling enemy not shooting at player 2d platformer

My enemy is shooting only when player is to the left of the enemy regardless of it it is facing the player or not. When enemy moves left to right it shoots in the opposite direction to that of player. Could anyone tell me where I'm going wrong and how to correct this.

Fireball code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Fireball : MonoBehaviour { public float speed; public int damage; public float lifeTime; private GameObject player;

 // Start is called before the first frame update
 void Start()
 {
     
     //delay , basically to define, after how many seconds would we like to destroy game object
     Destroy(gameObject, lifeTime); 
 }

 // Update is called once per frame
 void Update()
 {
     transform.Translate(Vector2.left * speed * Time.deltaTime);
 }

 private void OnTriggerEnter2D(Collider2D collision)
 {
     if (collision.tag == "Player")
     {
         collision.GetComponent<Player>().TakeDamage(damage);
     }
     Destroy(gameObject);
 }

}

Patrol code: using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Patrol : MonoBehaviour { public float speed; public float distance;

 private bool movingRight = true;

 public Transform groundDetection;
 

 private void Update()
 {
     transform.Translate(Vector2.right * speed * Time.deltaTime);
     int layer_mask = LayerMask.GetMask("Ground");
    
     RaycastHit2D groundinfo = Physics2D.Raycast(groundDetection.position, Vector2.down, distance, layer_mask); //origin, direction, distance
    

     if (groundinfo.collider == false)
     {
         if(movingRight == true)
         {
             transform.eulerAngles = new Vector3(0, -180, 0); //turn 180 degrees
             movingRight = false;
         }
         else
         {
             transform.eulerAngles = new Vector3(0, 0, 0);
             movingRight = true;
         }
     }
 }


}

shooting enemy code: using System.Collections; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using UnityEngine;

public class ShootingPlayer : MonoBehaviour {

 //Cached component references
 Animator enemyAnimator;

 //State
 [SerializeField] GameObject enemy;
 [SerializeField] GameObject fireBall;
 [SerializeField] Transform shotSpawnPoint;

 [SerializeField] float timeBetweenShots ;
 [SerializeField] float attackAnimationDuration;

 [SerializeField] float playerRange ;
 [SerializeField] Transform player;

 private float nextShotTime;
 private float endAttackAnimationTime;

 private void Awake()
 {
     enemy = GameObject.Find("Enemy");
     enemyAnimator = enemy.GetComponent<Animator>();
 }

 private void Start()
 {
     player = GameObject.FindGameObjectWithTag("Player").transform;
 }

 // Update is called once per frame
 void Update()
 {
     // draws line to detect player at a distance at the start and end of givem range, in this case behind and ahead of enemy
     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));

     
     if (player != null)
     {
         ShootAtPlayer();
     }

    
     if (enemyAnimator.GetCurrentAnimatorStateInfo(0).IsTag("Attacking") && Time.time > endAttackAnimationTime)
     {
         enemyAnimator.SetBool("Attacking", false);
     }
 }

 public void ShootAtPlayer()
 { 

     if (transform.localScale.x > 0 && player.transform.position.x < transform.position.x && player.transform.position.x > transform.position.x - playerRange && Time.time > nextShotTime)
     {
         Debug.Log("fireball going right");
         Instantiate(fireBall, shotSpawnPoint.position, shotSpawnPoint.rotation);
         enemyAnimator.SetBool("Attacking", true);
         nextShotTime = Time.time + timeBetweenShots;
         endAttackAnimationTime = Time.time + attackAnimationDuration;
     }

     // somehow never enters this loop

     if (transform.localScale.x < 0 && player.transform.position.x > transform.position.x && player.transform.position.x < transform.position.x + playerRange && Time.time > nextShotTime)
     {
         Debug.Log("fireball going left");
         Instantiate(fireBall, shotSpawnPoint.position, shotSpawnPoint.rotation);
         enemyAnimator.SetBool("Attacking", true);
         nextShotTime = Time.time + timeBetweenShots;
         endAttackAnimationTime = Time.time + attackAnimationDuration;
     }

     /* Continuous attacking

    if (Time.time > nextShotTime)
    {
        Instantiate(fireBall, shotSpawnPoint.position, shotSpawnPoint.rotation);
        enemyAnimator.SetBool("Attacking", true);
        nextShotTime = Time.time + timeBetweenShots;
        endAttackAnimationTime = Time.time + attackAnimationDuration;
    }
    */


 }

}

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

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

Raycast Enemy AI shooting script 1 Answer

Why does my shoot script break when I build the game 0 Answers

Help with enemy AI Please! (on 2D Platformer) 0 Answers

Enemy is shooting the opposite direction of player 1 Answer

Handling Slopes 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