- Home /
[Closed] Enemy Shooting isn't working correctly
Hello everyone. I have an enemy who moves back and forth and when the player is within range, the enemy stops, looks at the Player, and begins to shoot. It's very simple, but the shooting is off. I can't figure what I did wrong, but when the enemy shoots, he shoots towards the enemy, but at a 45 degree angle, so not at him really, just in his direction. If I go onto the other side of the enemy (it's a 2d sidescroller), then the enemy doesn't even shoot towards me on the other side. He just keeps shooting along the previous path. The enemy turns to face the Player if the players in range.
Here's where I find out the direction:
direction = CurrentPos - PlayerScript.pPos;
and here's my shoot function that gets called:
function Shoot(){
fire = false;
var firedbullet = Instantiate(bullet,transform.Find("EnemyBulletSpawn").position, transform.rotation);
yield WaitForSeconds(1.5);
fire = true; }
Thank you for any help you guys can give.
What does the script that moves the bullet look like? If you look in scene view is the blue arrow pointing at the player? What does the code that sets rotation look like?
Well, my sidescroller is working on the y and x axis, and my character and the bullet line up perfectly on the x-axis. Origianly I instantiated the bullet this way:
var instantiatedbullet = Instantiate(bullet,transform.Find("EnemyBulletSpawn").position, Quaternion.Euler(direction.normalized));
I saw somewhere that someone had the above code for instantiating so I tried that with no success. I find "direction" by doing this:
direction = PlayerScript.pPos - transform.position;
$$anonymous$$y bullet move script is as follows:
var bullet$$anonymous$$oveSpeed: int = 25; var lifetime: float = 0.8; var lx; var ly;
static var Hit = false;
function Update () {
transform.Translate(Vector3(lx,ly,0)* bullet$$anonymous$$oveSpeed * Time.deltaTime);
}
function Awake(){
var xdir: float = PlayerScript.pPos.x - EnemyScript.EnemyPos.x;
var ydir: float = PlayerScript.pPos.y - EnemyScript.EnemyPos.y;
var angle: float = $$anonymous$$athf.Atan2(xdir,ydir);
lx = $$anonymous$$athf.Sin(angle);
ly = $$anonymous$$athf.Cos(angle);
Destroy(gameObject, lifetime);
}
function OnTriggerEnter(other : Collider){
if (other.tag == "Player")
{ Hit = true; Destroy(gameObject); }
}
Well your original version should have been:
var instantiatedbullet = Instantiate(bullet,transform.Find("EnemyBulletSpawn").position, Quaternion.LookRotation(direction));
If direction is the difference between the two locations.