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 /
  • Help Room /
avatar image
0
Question by SteenPetersen · May 13, 2017 at 03:36 PM · rotation2d gameprojectile

Rotate projectile sprite in the direction it is flying (like an arrow) 2D.

Hi guys,

This questions seems to have been answered here a few times but the solutions dont work for me for some reason and I wanted to just post my issue here see if maybe I can clear this up.

I have a monster in my 2d game, when a player enters his collider he fires a projectile at time. Simple. However the spite of the fireball is always facing right. so although it flys in the correct direction, the sprite is facing the wrong direction.

here is my script to shoot at the player:

 public void OnCastComplete()
     {
         Rigidbody2D projectile;
 
         projectile = Instantiate(fireball, projectileInitialLocation.transform.position, transform.rotation) as Rigidbody2D;
 
         projectile.transform.LookAt(currentTarget.transform);
 
         projectile.velocity = projectile.transform.forward * projectileSpeed;
     }

thank you for your input.

Comment
Add comment · Show 1
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 SteenPetersen · May 13, 2017 at 05:21 PM 0
Share

I have tried adding a calculation to deter$$anonymous$$e the rotation of the sprite as so:

         Vector2 dir = currentTarget.transform.position - projectileInitialLocation.transform.position;
         float angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
         projectile.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

but seems to do nothing even though I have read that it should rotate the sprite.

I I ins$$anonymous$$d write:

         Vector2 dir = currentTarget.transform.position - transform.position;
         float angle = $$anonymous$$athf.Atan2(dir.y, dir.x) * $$anonymous$$athf.Rad2Deg;
         transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

so removing the projectile from the final line it rotates the monster that is shooting and not the projectile. kind of lost here.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SteenPetersen · May 13, 2017 at 06:05 PM

Found the problem, Because my game has a camera that looks at a 25 degree angle or there about down onto the world each sprite has a script like this on it:

 public class FaceCamera : MonoBehaviour {
 
     public Transform target;
 
     void Start () {
         target = GameObject.Find("MainCamera").GetComponent<Transform>();
     }
     
     void Update () {
 
         if (target != null)
 
             this.transform.rotation = Camera.main.transform.rotation;
     }
 }

this was causing the issue.

the next question now is. How do I make the sprites face the camera like the do in this script but at the same time rotate only on their z axis when flying towards their target. because that would work.

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

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

Related Questions

Problem with projectile rotation - 2D 0 Answers

Hi . i am making a ninjump like game and i am having trouble on the start with with player sprite jumping script. 1 Answer

Having projectile face mouse position 1 Answer

Is there a way to rotate a parent without rotating the children? 0 Answers

Firing a projectile change the direction of the other projectiles. 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