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
1
Question by Ibzy · Jan 30, 2014 at 02:44 PM · instantiatefpsaienemy

Instantiate Projectile towards player (regardless of transform.rotation)

I have an enemy in my game who (as you'd expect) shoots at me if I'm in range. I have a gun attached to the enemy model, and a script which instantiates a projectile at the end of it(using a bulletSpawn empty GameObject). The problem is, as part of the shooting animation the gun isn't facing directly at me.

How can I instantiate the bullet in the direction of the player?

Thanks

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ArkaneX · Jan 30, 2014 at 04:38 PM

 var direction = enemy.transform.position - projectileStartPosition;
 var projectile = (Transform)Instantiate(projectilePrefab, projectileStartPosition, Quaternion.LookRotation(direction));

Or, alternatively, you can call

 projectile.LookAt(enemy.transform);

just after instantiating.

Comment
Add comment · Show 12 · Share
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 Ibzy · Jan 30, 2014 at 10:05 PM 0
Share

I've tried both of the above and I'm getting the same issue - which I have just realised I failed to mention initially - the 'bullet' appears to be firing past my(the player character) left shoulder. Is there something I need to change to have it direct to the center of my player transform?

avatar image supermap · Jan 31, 2014 at 04:24 AM 0
Share

it must be that the centre of your player is at the left shoulder.

now you could either find a way to move the centre of your character to the actual centre,

or you could do it the weird way and add a child empty gameobject of the character at the centre and aim at that,

or you could even set an offset for the position you are considering for the character and adjust it depending on the direction your character

i would try them in order... the last one seems quite last resort

avatar image Ibzy · Feb 01, 2014 at 01:54 PM 0
Share

I've tried everything mentioned here and still the shot is being fired in the wrong direction. Attached is a screen capture to show where the bullet is going compared to the center position.

 Transform bullet = Instantiate(bulletPrefab, bulletSpawn.position, Quaternion.LookRotation((PlayerCenter.position - bulletSpawn.position))) as Transform;

The above is my instantiation, and PlayerCenter is found via:

 Player = GameObject.FindGameObjectWithTag("Player");
         PlayerCenter = Player.transform.FindChild ("Center");

I now have a blank GameObject positioned at the center of my player character.

Shot example

Thanks

shot.png (299.9 kB)
avatar image ArkaneX · Feb 02, 2014 at 07:24 PM 0
Share

Could you compare bullet.forward to normalized vector of direction from spawn point to player center, and see if it shows true or false?

 Debug.Log(bullet.forward == (PlayerCenter.position - bulletSpawn.position).normalized);
avatar image Ibzy · Feb 02, 2014 at 09:51 PM 0
Share

ArkaneX - the above debug shows true. I have noticed that while the enemy is shooting (in the state of shooting at me, not during the shot itself) if I move slightly to the left/right the bullet direction doesn't change, meaning if I move enough to the left I can have the bullet skim over my right shoulder and not collide (the above screenshot collides as expected). Thanks

Show more comments
avatar image
1

Answer by supermap · Jan 30, 2014 at 04:45 PM

You substract the position of the bullet from the position of the player. That should give you a vector in the direction the bullet should take.

Normalize it then multiply it by the speed you want and you have your vector for the velocity of the bullet.

Comment
Add comment · Share
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

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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

FPS Problem Help! 0 Answers

Hurting enemy when attacking status is equal to true and player is colliding with enemy object. 1 Answer

transform set for an enemy to follow doesnt work when enemies are spawned 1 Answer

Deal damage on collision 2 Answers

Ai Alien Zombies get stuck on trees? 0 Answers


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