Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by timo0060 · Jun 20, 2012 at 01:32 PM · enemyshootingsidescroller

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

Comment
Add comment · Show 18
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image timo0060 · Jun 20, 2012 at 04:38 PM 0
Share

Any one have any helpful information?

avatar image timo0060 · Jun 20, 2012 at 08:55 PM 0
Share

I'm sure it's a simple solution.

avatar image whydoidoit · Jun 20, 2012 at 08:58 PM 0
Share

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?

avatar image timo0060 · Jun 20, 2012 at 09:18 PM 0
Share

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); }

}

avatar image whydoidoit · Jun 20, 2012 at 09:28 PM 0
Share

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.

Show more comments

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Enemies Shoot Towards Face, Would Like to have them Shoot to Chest 1 Answer

enemy follow player 2d sidescroller rpg 0 Answers

Get enemy to fire at me from a high position 0 Answers

AirStrike Scripting Help 0 Answers

How to make Enemy shoot at Player Top Down 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges