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 /
avatar image
0
Question by rOBY GAMES · May 19, 2015 at 07:06 PM · playerenemyshoot

C # script Enemy shoots left 2d platform.

Hello in the title as my enemy fire only left when facing the player. When the player is on the right, the enemy does not shoot. I found the problem in this line of code # 32 --- _direction = new Vector2 (-1, 0); Changing the line of code in this way --- _direction = new Vector2 (1, 0); The enemy shoot right.

I ask you to help to shoot the enemy, both right and left.

scipt:

 using UnityEngine;
 
 public class EnemyShoot : MonoBehaviour, ITakeDamage, IPlayerRespawnListener 
     
     
 {
     public float Speed;
     public float FireRate = 1;
     public Projectile Projectile;
     public GameObject DestroyedEffect;
     public int PointsToGivePlayer;
     public AudioClip ShootSound;
     public Animator anim;
     
     
     private CharacterController2D _controller;
     private Vector2 _direction;
     private Vector2 _startPosition;
     private float _canFireIn;
     
     int jumpHash = Animator.StringToHash("Enemy Walk");
     
     public void Start ()
         
     {
         
         anim = GetComponent<Animator>();
         
         _controller = GetComponent<CharacterController2D>();
         
         
         _direction = new Vector2(-1, 0);
         
         
         _startPosition = transform.position;
         
     }
     
     
     public void Update () 
     {
         
         _controller.SetHorizontalForce(_direction.x * Speed);
         
         if ((_direction.x < 0 && _controller.State.IsCollidingLeft) || (_direction.x > 0 && _controller.State.IsCollidingRight)) 
         {
             _direction = -_direction;
             transform.localScale = new Vector3(-transform.localScale.x, transform.localScale.y, transform.localScale.z);
             
         }
         
         
         
         if ((_canFireIn -= Time.deltaTime) > 0)
             return;
         
         var raycast = Physics2D.Raycast(transform.position, _direction, 10, 1 << LayerMask.NameToLayer("Player"));
         if (!raycast)
             return;
         
         var projectile = (Projectile) Instantiate(Projectile, transform.position, transform.rotation);
         projectile.Initialize(gameObject, _direction, _controller.Velocity);
         _canFireIn = FireRate;
         
         if (ShootSound != null)
             AudioSource.PlayClipAtPoint(ShootSound, transform.position);
         
     }
     
     public void TakeDamage(int damage, GameObject instigator)
     {
         if (PointsToGivePlayer != 0)
         {    
             var projectile = instigator.GetComponent<Projectile>();
             if (projectile != null && projectile.Owner.GetComponent<Player>() != null)
             {
                 GameManager.Instance.AddPoints(PointsToGivePlayer);
                 FloatingText.Show(string.Format("+{0}!", PointsToGivePlayer), "PointStarText", new FromWorldPointTextPositioner(Camera.main, transform.position, 1.5f, 50));
             }
         }
         
         Instantiate(DestroyedEffect, transform.position, transform.rotation);
         gameObject.SetActive(false);
         
     }
     
     public void OnPlayerRespawnInThisCheckpoint(Checkpoint checkpoint, Player player)
         
     {
         _direction = new Vector2(-1, 0);
         transform.localScale = new Vector3(1, 1, 1);
         transform.position = _startPosition;
         gameObject.SetActive(true);
         
     }
 }
 
 
 
 
 
 
 
 
 
 
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Basen · May 19, 2015 at 07:39 PM

  if ((_direction.x < 0 && _controller.State.IsCollidingLeft) || (_direction.x > 0 && _controller.State.IsCollidingRight)) 

This if statement should be your problem I guess. Since you have two conditions in there(left or right) however the result is always the same. Do it like this :

 if (_direction.x < 0 && _controller.State.IsCollidingLeft)
 {
 shoot left
 }
 else if(_direction.x > 0 && _controller.State.IsCollidingRight)
 {
 shoot right

}

Comment
Add comment · Show 1 · Share
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
avatar image rOBY GAMES · May 19, 2015 at 07:58 PM 0
Share

Hello Basen.

I entered your code.

 if ((_direction.x < 0 && _controller.State.IsCollidingLeft) || (_direction.x > 0 && _controller.State.IsCollidingRight)) 


But now the enemy does not shoot or to the left and right.

then this code gives me 2 errors.

if (_direction.x < 0 && _controller.State.IsCollidingLeft) { shoot left } else if(_direction.x > 0 && _controller.State.IsCollidingRight) { shoot right

}

Error Code:

1) error CS1525: Unexpected symbol }', expecting )', ,', ;', [', or =' 2) error CS1525: Unexpected symbol }', expecting )', ,', ;', [', or ='

avatar image
0

Answer by rOBY GAMES · May 19, 2015 at 08:13 PM

Now the enemy shoots at all with this code.

  if (_direction.x < 0 && _controller.State.IsCollidingLeft)
  {
  shoot left
  }
  else if(_direction.x > 0 && _controller.State.IsCollidingRight)
  {
  shoot right

We must change this line --- _direction = new Vector2 (-1, 0);

Only in this way the enemy shoots.

 }
Comment
Add comment · Share
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

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

20 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

Related Questions

No collision on trigger and character controller 1 Answer

make Enemy shoot at player 2 Answers

Problem with enemy shooting 4 Answers

Player damage stops working over time 0 Answers

Kill the Player? 1 Answer


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