• 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 mlaveg · Jul 07, 2018 at 08:57 AM · 2drotationtransformquaternionstranslation

Translating a transform also rotates it (Unity 2017.3.1f1)

I'm building a little scene in 2D. My player game object is a sphere sprite and to it is attached the script that you can see below. The player can shoot projectiles which are visualized as a stretched sprite.

For now, I only want to place the projectile in correct orientation above, right, below, and left of the player game object. Because the projectile sprite has a horizontal orientation by default, I'm only modifying the rotation for the UpArrow and DownArrow keys.

Both when I only rotate the projectile transform (left picture below) and when I only translate it (right picture below), it works as expected. (On the left I selected the correctly oriented projectile sprite because it wouldn't be visible otherwise) alt text
But when I rotate and then translate the projectile transform, the translation does not move it where it should but in the angle that I supplied for the rotation (intended position in red)alt text

This script is attached to my player game object:

 public class Shoot : MonoBehaviour {
     
         public GameObject projectilePrefab;
         GameObject projectile = null;
     
         private void Update()
         {
             if (Input.GetKeyDown(KeyCode.RightArrow)){
                 projectile = Instantiate(projectilePrefab);
                 projectile.transform.Translate(Vector2.right);
             }
             if (Input.GetKeyDown(KeyCode.LeftArrow))
             {
                 projectile = Instantiate(projectilePrefab);
                 projectile.transform.Translate(Vector2.left);
             }
             if (Input.GetKeyDown(KeyCode.UpArrow))
             {
                 projectile = Instantiate(projectilePrefab);
                 projectile.transform.rotation = Quaternion.AngleAxis(90, Vector3.forward);
                 projectile.transform.Translate(Vector2.up);
             }
             if (Input.GetKeyDown(KeyCode.DownArrow))
             {
                 projectile = Instantiate(projectilePrefab);
                 projectile.transform.rotation = Quaternion.AngleAxis(90, Vector3.forward);
                 projectile.transform.Translate(Vector2.down);
             }
         }
 }


rotation-translation2.png (5.4 kB)
rotation-l-and-translation-r.png (223.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

1 Reply

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

Answer by mlaveg · Jul 08, 2018 at 12:13 PM

I hadn't been aware that transform.Translate() by default translates along the transform's own Vector2.up which is modified by

 projectile.transform.rotation = Quaternion.AngleAxis(90, Vector3.forward);


Writing

 projectile.transform.Translate(Vector2.up, Space.World);

and

 projectile.transform.Translate(Vector2.down, Space.World);

respectively solves it.

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.

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

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

Related Questions

Can Unity make this kind of scale/transform? 1 Answer

Animation changes constantly reset themselves. 1 Answer

Smooth rotate between changing rotations (already using Lerp, problems arise) 0 Answers

Making 2D Compass From Quaternion 1 Answer

Rotation Of Transform Does Not Result In Correct Value 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