- Home /
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);
}
}
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
}
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
='
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.
}
Your answer
Follow this Question
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