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 TurboHermit · Apr 15, 2012 at 01:40 AM · instantiaterotatelookatface

Rotate to Center of Screen

Just a quick little question I think. I'm instantiating bullets that apply force to themselves and it works perfectly. But I want them to when instantiated, face the center of the screen.

So basically when they fire they will initially go to the center of the screen. (Unless you move.)

I thought this might be solved by rotating the bullet when it's spawned or even in the instantiate settings, so it will face the center of the screen. Anyone any suggestions on how to do this?

Comment
Add comment · Show 2
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 TurboHermit · Apr 15, 2012 at 08:30 PM 0
Share

Well I don't want the bullet to look at an object itself. I don't want a child object for the camera either. $$anonymous$$y camera can be a little rickety because of my custom scripts. I want it to face the center of the screen.I thought I could maybe solve this with ScreentoWorld or something.

avatar image TurboHermit · Apr 16, 2012 at 07:45 AM 0
Share

Well it's 3d person and the bullet spawns at the character, it would look rather weird if the bullet spawns exactly in front of the camera ins$$anonymous$$d. How can I solve this in a different way?

I have an idea how I could fix this but I'd rather try a different approach. I thought maybe an object that's exactly in the middle of the camera but always clamps to the object that's exactly in front of it, no matter how far the distance. Then I'd have the bullet face this target object so it will go there. Is this a solid approach?

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Apr 15, 2012 at 09:38 PM

The screen is a 2D space, thus its center in the 3D world is actually a line starting at the camera position and going in the camera forward direction.
If you can instantiate the bullet at the camera position, things are easier:

 var camTransf = Camera.main.transform;
 var bullet = Instantiate(bulletPrefab, camTransf.position, camTransf.rotation);
 ...

But if the bullet is instantiated at any other point outside the "screen center line", its trajectory can at most cross the center line at some point. If you want this point to be at 100 units from the camera, for instance, you could use this:

 // create the bullet with zero rotation...
 var bullet = Instantiate(bulletPrefab, transform.position, Quaternion.identity);
 // then rotate it to the screen center at 100 units:
 bullet.transform.LookAt(Camera.main.ViewportToWorldPoint(Vector3(0.5, 0.5, 100))); 
 ...

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 rutter · Apr 15, 2012 at 01:45 AM

If you know where you want the bullet to face, you can call `LookAt()` on its transform component. You can make that call every time you want the bullet to change directions (once right after its spawned, once every frame, or some mix).

You can see some simple examples at the page I've linked, or at this page from the Unity manual, which covers spawning and facing projectiles.

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

7 People are following this question.

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

Related Questions

How do I make one GameObject Point Towards another GameObject in 2D? 1 Answer

Instantiated Object - Look At. 1 Answer

Rotating a GameObject 180 to face another Gameobject on single axis 0 Answers

How do I make my player face the way he walks? 2 Answers

Preventing "LookAt" from flipping? 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