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 Rickywild · Feb 07, 2017 at 12:50 PM · rotation2d-platformerprojectileeuleranglesprojectiles

2D Weapon Rotation Behaviour Changes on Flipping Sprite?

Hi,

I have a player and that object has a weapon. The weapon can be rotated when it's a crossbow. So the player can aim and shoot. I've noticed when I face left, aim with weapon and then shoot, the projectile seems to shoot in the opposite direction it'd go, as opposed to facing right (where the project shoots in the direction you expect)

I use something like this for rotating,

 if(input.y >= 0.1f)
 {
             myTransform.Rotate (Vector3.forward * 8);//-90);
             crossBowAngle = this.gameObject.transform.eulerAngles;
 }
 
 if(input.y <= -0.1f)
 {
             myTransform.Rotate (Vector3.forward * -8);//-90);
             crossBowAngle = this.gameObject.transform.eulerAngles;
 }

This code seems to work as intended and is based on the weapon obj which is a separate obj/script to the projectile.

The projectile simply has,

         if(dirRight)
         {
             if(flipped)
             {
                 Flip();
                 flipped = false;
             }
 
             velocity.x = Time.deltaTime * speed;
         }
         else
         {
             if(!flipped)
             {
                 Flip();
                 flipped = true;
             }
 
             velocity.x = -(Time.deltaTime * speed);
         }
 
         myTransform.Translate(velocity);

The flipping here is just to ensure the projectile sprite doesn't appear to be shot out backwards.

I believe the problem lays within my actual player script where, if i face left or right, I flip the player using the following,

     private void Flip()
     {
 
         //Flip the sprite to face the correct direction accordingly.
         playerState.facingRight = !playerState.facingRight;
         Vector3 theScale = myTransform.localScale;
         theScale.x *= -1;
         myTransform.localScale = theScale;
 
 
         GameManager.playerDirRight = playerState.facingRight;
         
     }


Any ideas? So just to make it clear, when facing right, if i aim up or down and then shoot, the projectile will shoot just fine if the direction as expected.

When facing left however, if i aim up, you can expect the projectile to be shot down instead. If i aim down, it will be shot up. Can anyone see anything in the code i provided that may be causing this issue? Thank you in advance

Comment
Add comment · Show 4
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 avazutaev · Feb 07, 2017 at 01:14 PM 0
Share

Don't you try to use something like this (for your shoots): rb.AddForce(transform.forward * thrust);

avatar image Rickywild avazutaev · Feb 07, 2017 at 01:20 PM 0
Share

Don't use rigidbodys, get far to many physics spikes in profiler. Adding to velocity then putting it through myTransform.Translate(velocity); works. Well when facing right anyway, maybe that's where the fault lies

avatar image avazutaev Rickywild · Feb 07, 2017 at 01:33 PM 0
Share

So, try to multiply your velocity with Vector2.right or left so far

Show more comments

1 Reply

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

Answer by Rickywild · Feb 07, 2017 at 03:17 PM

Ok I got it. Due to flipping the rotation was some what flipped. Studying the values of the projectiles in the inspector, I noticed if i applied a negative to the value of the z rotation, it'd go in the desired direction (when facing left,which was the problem here).

Below code fixed it for me (probably a little hacky but it works)

     void OnEnable()
     {
         dirRight = GameManager.playerDirRight;
 
         if(!dirRight)
         {
             myTransform = this.transform;
             Vector3 angle = myTransform.rotation.eulerAngles;
             angle.z = -angle.z;
             Quaternion q = myTransform.rotation;
             q.eulerAngles = angle;
             myTransform.rotation = q;
 
         }
     }

It's in OnEnable() because of obj pooling.

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

106 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

Related Questions

Help! I can't get this code to function correctly 1 Answer

problem setting rotation 1 Answer

Problem with projectile rotation - 2D 0 Answers

Smooth rotation on Y axis to target pose 0 Answers

How to convert Quaternions to Euler angles with different order? 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