Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Sp33d · Oct 18, 2015 at 09:28 AM · rotationinstantiatetransform.rotation

Have object face towards player when instantiated

I have a sentry gun firing at the player's current position. This works fine, but I'd like the projectile to be rotated towards the player too when it's instantiated. A script attached to the turret instantiates the fireball:

 Rigidbody2D fireProjectile = Instantiate (fireproj, new Vector3 (transform.position.x, transform.position.y, transform.position.z), transform.rotation) as Rigidbody2D;

How can I get transform.position to be the current position 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

Answer by Eno-Khaon · Oct 18, 2015 at 10:30 AM

First off, you can probably simplify your argument of

 ...new Vector3 (transform.position.x, transform.position.y, transform.position.z)...

to a simple

 ...transform.position...

in this case.

Second, there appear to be no inherent problems with your lines unless the projectile is not oriented in the same way as your turret. For example, if the bullet and turret both naturally face upward, then there shouldn't be any problem. If the turret faces right and the bullet faces left, then you'll need to flip it around.

For Quaternions, that can be handled in as simple-looking of a presentation as:

 transform.rotation * offsetRotation

As for the definition of "offsetRotation", continuing on the basis of Quaternion values, you may consider tinkering with Quaternion.AngleAxis() for that.

On another note, you ask about the position of the player at the end. If your sentry gun is tracking the player's position already, then you would already have that information on-hand. If not, then there are numerous ways of getting that information.

If your turret does not rotate to face the player, but you wish for the projectiles to do so, well, looks like I wasn't the first one to address that concept of rotation, either.

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
avatar image
0

Answer by bgprocks · Oct 18, 2015 at 12:45 PM

Direction = player.position - projectile.position. This will send your projectile straight to player. The just use the Unity Transform.lookat(player). Together your projectile will go the right way, and face the right direction.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Rotation Problem 1 Answer

Instantiate an image, equally in a circle and rotate it away from the center? 2 Answers

How can I align axis with quaternion.fromToRotation? 1 Answer

Instantiating an object with the correct rotation 1 Answer

Unity 2D Apply force in Z Direction 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