x


[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.

more ▼

asked Jun 20 '12 at 01:32 PM

timo0060 gravatar image

timo0060
129 11 25 28

Any one have any helpful information?

Jun 20 '12 at 04:38 PM timo0060

I'm sure it's a simple solution.

Jun 20 '12 at 08:55 PM timo0060

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?

Jun 20 '12 at 08:58 PM whydoidoit

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;

My bullet move script is as follows:

var bulletMoveSpeed: int = 25; var lifetime: float = 0.8; var lx; var ly;

static var Hit = false;

function Update () {

transform.Translate(Vector3(lx,ly,0)* bulletMoveSpeed * 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 = Mathf.Atan2(xdir,ydir);  

lx = Mathf.Sin(angle);
ly = Mathf.Cos(angle);  

Destroy(gameObject, lifetime);  

}

function OnTriggerEnter(other : Collider){

if (other.tag == "Player")

{ Hit = true; Destroy(gameObject); }

}

Jun 20 '12 at 09:18 PM timo0060

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.

Jun 20 '12 at 09:28 PM whydoidoit
(comments are locked)
10|3000 characters needed characters left

0 answers: sort voted first
Be the first one to answer this question
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x652
x274
x70

asked: Jun 20 '12 at 01:32 PM

Seen: 501 times

Last Updated: Jun 22 '12 at 01:43 PM