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 RoR · Oct 29, 2010 at 07:38 AM · transform

Question regarding Transforms

Any good sources for undersatnding transforms? How do I get things to go diagonal, towrds a position?

Thanks

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
2
Best Answer

Answer by TheDemiurge · Oct 29, 2010 at 11:46 AM

The transform does two things: First, it lets a game object keep track of its position and rotation (both in relation to a parent object, and to the world), and its scale.

Second, it lets the object change and manipulate those values, in multiple ways.

The transform's position member is a Vector3 type, which means it's got x, y and z member variables. For example if you have an object located at (0, 10, 0), this means when you access transform.position, its members (x, y and z) will equal to 0, 10 and 0, respectively. For the rotation, rather than accessing transform.rotation you'd access transform.eulerAngles, because rotation is a Quaternion which is a whole other demon (a good, yet often misunderstood demon), and for that you'd never modify rotation or localRotation yourself. If another object was then attached to this one and centered to it, its transform.localPosition would be (0, 0, 0), and its transform.localEulerAngles would be (0,0,0) because it's not moved or rotated relative to its parent object, only to the game world. If you then move this child object 10 units upward, its local position would show (0, 10, 0) and its position would show (0, 20, 0). And so forth.

To move the object itself you use the Translate function, and to rotate it you use Rotate.

When you pull up the Transform class in the scripting manual, you'll see a few examples of movement & rotation there. By default these two functions do their thing in relation to the local axes. What this means is that the easiest way to move forward is to use transform.Translate(Vector3.forward); where Vector3.forward is shorthand for (0, 0, 1). With rotations, you just need to remember that the rotation is done around the axis you specify. So transform.Rotate(Vector3.right) will make the object spin forward.

Figuring out a specific way to move or rotate an object is often a question of math. It takes a basic understanding of vector math, and of some other things you can get from the Vector3 class. For example, when you add two vectors together you get a vector pointing somewhere between the two and at a length that depends on the two. Take a look at the following code.

var vec1: Vector3 = Vector3(0, 0, 1); // forward vector var vec2: Vector3 = Vector3(1, 0, 0); // right vector var newVec: Vector3 = vec1 + vec2; // (1, 0, 1)

function Update() { transform.Translate(newVec * Time.deltaTime); // move forward and to the right }

We took two vectors whose length was 1 and added them together. This creates a new vector angled 45 degrees to both and with a length equal to the square root of 2 (Pythagoras). If you then translate an object by this vector you'd get movement almost 1.5 times as fast as if you were using one of the other two. To solve this use something like var newVec: Vector3 = (vec1 + vec2).normalized;. This brings the vector back down to a length of 1.

If for example vec1 was (0, 0, 2), then adding vec1 and vec2 together would yield a vector pointing at (1, 0, 2) with a length of the square root of 5, which is angled 63.44 degrees to vec1 and 26.56 degrees to vec2.

Best way to grasp it better is to just read up on the other members of Vector3, Quaternion and Transform and experiment.

Comment
Add comment · Show 2 · 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 RoR · Oct 29, 2010 at 01:26 PM 0
Share

Thank you very much for your advice and information. Greatly Appreciated. I will go about that and experiment with Vector3, Quaternion and Transform. Thank you.

avatar image TheDemiurge · Oct 29, 2010 at 02:03 PM 0
Share

No problem. Best of luck to ya :)

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

No one has followed this question yet.

Related Questions

Parenting GameObject in the scene by scripts. 2 Answers

I need to know how to rotate a gameobject in all directions. 0 Answers

Adding Rotation to a Gameobject 1 Answer

What is wrong with this code? (Solved) 0 Answers

Unity Network Transform Not Syncing for Clients [UNet] 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