Enemy Shoots at opposite direction of player.
Im trying to make a 2D platformer where the enemy shoots at player when the player is in range. the problem is that the enemy shoots but in the opposite direction of the player. Even if i go to the right or left its still to the opposite side.
This is my script.
using UnityEngine;
using System.Collections;
public class ShootAtPlayerInRange : MonoBehaviour {
public float playerRange;
public GameObject enemyBullet;
public PlayerController player;
public Transform launchPoint;
public float waitBetweenShots;
private float shotCounter;
// Use this for initialization
void Start ()
{
player = FindObjectOfType<PlayerController> ();
shotCounter = waitBetweenShots;
}
// Update is called once per frame
void Update ()
{
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));
shotCounter -= Time.deltaTime;
if (transform.localScale.x < 0 && player.transform.position.x > transform.position.x && player.transform.position.x < transform.position.x +playerRange && shotCounter < 0)
{
Instantiate (enemyBullet, launchPoint.position, launchPoint.rotation);
shotCounter = waitBetweenShots;
}
if (transform.localScale.x > 0 && player.transform.position.x < transform.position.x && player.transform.position.x > transform.position.x -playerRange && shotCounter < 0)
{
Instantiate (enemyBullet, launchPoint.position, launchPoint.rotation);
shotCounter = waitBetweenShots;
}
}
}
I think the error is in the update function, If you are able to help i will be grateful. Thankyou.
Hi ! Where does the bullet movement come from ? Has it a script attached ?
Sorry for late reply as i was busy but yes there is a script, this is it -
using UnityEngine;
using System.Collections;
public class EnemyBulletControl : $$anonymous$$onoBehaviour {
public float speed;
public PlayerController player;
//public GameObject enemyDeathEffect;
public GameObject impactEffect;
//public int pointsFor$$anonymous$$ill;
public int damageToGive;
// Use this for initialization
void Start ()
{
player = FindObjectOfType<PlayerController> ();
if (player.transform.position.x < transform.position.x)
speed = -speed;
}
// Update is called once per frame
void Update ()
{
GetComponent<Rigidbody2D> ().velocity = new Vector2 (speed, GetComponent<Rigidbody2D> ().velocity.y);
}
void OnTriggerEnter2D (Collider2D other)
{
if (other.name == "Player")
{
//Instantiate(enemyDeathEffect, other.transform.position, other.transform.rotation);
//Destroy (other.gameObject);
//Score$$anonymous$$anager.AddPoints(pointsFor$$anonymous$$ill);
Health$$anonymous$$anager.HurtPlayer (damageToGive);
}
Instantiate (impactEffect, transform.position, transform.rotation);
Destroy (gameObject);
}
}
Your answer
Follow this Question
Related Questions
Tagging a collider and destroying it 1 Answer
How do i make an ai shoot? 2D 1 Answer
How make the "player" shoot on the Y axis? 0 Answers
Enemies cannot kill Player. 0 Answers
How to damage player when bullet collides with player. 0 Answers