• 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 wijesijp · Aug 29, 2013 at 09:35 AM · rotation

Rotate bullet

In my game I have the camera 45 degrees rotated around x axis, looking down at game area.
I have gun turret and it fires bullet prefabs created from 2D sprites.

Sprite image is as shown below.

alt text

I get the direction vector to the target,

 direction        = (target - startPosition).normalized;

But I can’t get the bullet sprite to rotate to the correct orientation.

Can anyone provide some help?

bullet.png (4.2 kB)
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
0
Best Answer

Answer by robertbu · Aug 29, 2013 at 10:35 AM

This is an ugly rotation problem. Assuming you are using the built-in plane to display the bullet, this might work:

 bullet.transform.right = -(target - startPosition);  
 var q = Quaternion.FromToRotation(bullet.transform.up, -Camera.main.transform.forward);
 bullet.transform.rotation = q * bullet.transform.rotation; 
Comment
Add comment · Show 5 · 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 wijesijp · Aug 29, 2013 at 10:49 AM 0
Share

I think we are pretty close to a solution.

The bullet is now oriented in a way that it points to the target. However its top edge orientation is also perpendicular to the camera. So the bullet is not visible now.

avatar image robertbu · Aug 29, 2013 at 11:28 AM 0
Share

I assume you are not using the built-in plane but something else? If you are using vertical plane from the Wiki CreatePlane script, then line 2 should be:

 var q = Quaternion.FromToRotation(bullet.transform.forward, Camera.main.transform.forward);
avatar image wijesijp · Aug 29, 2013 at 11:44 AM 0
Share

I am using 2d toolkit for sprite.

I have modified my prefab such that now it points forward (+z)

Now when I use, transform.LookAt(target); It points exactly at the target.

But because of the camera angle part of the bullet sprite face is not visible.

If I can rotate the bullet from this position to face the camera bullet image will be fully visible. How can I use FromToRotation for this?

avatar image robertbu · Aug 29, 2013 at 11:50 AM 0
Share

You need to figure out what vector is the face of the prefab. If your prefab points forward, then it is likely transform.right. The lines of code you want are:

 var q = Quaternion.FromToRotation(bullet.transform.right, -Camera.main.transform.forward);
 bullet.transform.rotation = q * bullet.transform.rotation;

If you don't know which side, then just substitute the six possibilities for bullet.transform.right to see which one works.

 transform.right
 -transform.right
 transform.up
 -transform.up
 transform.forward
 -transform.forward
avatar image robertbu · Aug 29, 2013 at 11:52 AM 0
Share

Oh, and when you figure out which one, you may want to establish that side as the up vector in the LookAt(). Assu$$anonymous$$g it is transform.right, your lookAt becomes:

 transform.LookAt(target, transform.right);
avatar image
1

Answer by robhuhn · Aug 29, 2013 at 10:06 AM

What about lookAt?

 bullet.transform.LookAt(target);


Comment
Add comment · Show 1 · 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 wijesijp · Aug 29, 2013 at 10:17 AM 0
Share

when i use lookat it does rotate. lookat rotate the bullet sprite perpendicular to the directional vector.

The problem is i am using a 2d sprite pointing to negative x axis as my prefab.

The game I am building is a tower defense like game.

It seems we have to do something else to get it to look right

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Flip over an object (smooth transition) 3 Answers

How can I identify a non-uniform scaled mesh, and fix it? 1 Answer

transform position y changes when character rotates 0 Answers

eliminating angular velocity when clamp value hit? 0 Answers

raycast to determine pivot 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