Quantcast
Channel: Questions in topic: "enemy"
Viewing all articles
Browse latest Browse all 1488

C # script Enemy shoots left 2d platform.

$
0
0
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(); _controller = GetComponent(); _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(); if (projectile != null && projectile.Owner.GetComponent() != 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); } }

Viewing all articles
Browse latest Browse all 1488

Trending Articles