Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by godoy · May 08, 2018 at 08:32 PM · rotatetranslate

Translate not working properly

My problem seems easy, but I'm 3hrs trying to resolve... Let's go:

In my scene I have a Cube and a script (called Cube.cs) in C#.

All I want to do is rotate and translate the cube, in Cube.cs I have:

//

transform.eulerAngles = new Vector3(-30f, 0f, 0f); //OK

transform.Translate(Vector3.forward * 3f); //here is the problem

//

What's happening:

//

After the rotation, when I do the Translate, the Cube (GameObject) does not move 3 units, but 2.52..., if I not rotate the cube then the cube Translate right, but when I rotate the cube the cube not translate the 3 units. I appreciate all help to solve this.

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 Cherno · May 08, 2018 at 08:55 PM 0
Share

Are you sure? Try printing the distance between the start and end points to console.

 Vector3 start = transform.position;
 transform.eulerAngles = new Vector3(-30f, 0f, 0f); //O$$anonymous$$
 transform.Translate(Vector3.forward * 3f); //here is the problem
 Debug.Log(Vector3.Distance(start , transform.position));
avatar image TreyH · May 08, 2018 at 09:31 PM 0
Share

Are you sure there's nothing else influencing your object? This works fine:

 using UnityEngine;
 
 public class $$anonymous$$oveSome : $$anonymous$$onoBehaviour {
 
     void Awake () {
         Vector3 previous = this.transform.position;
 
         transform.eulerAngles = new Vector3(-30f, 0f, 0f); 
         transform.Translate(Vector3.forward * 3f); 
 
         Debug.LogFormat ("I moved {0} units", (this.transform.position - previous).magnitude);
     }
 }


I think you're reading the object's position as Z = 2.598, but the Y = 1.5. That is a total of 3 units.

avatar image godoy TreyH · May 09, 2018 at 01:30 PM 0
Share

Yes, I didn’t realize that translating (locally) after rotation, the units translated were affected by the rotation. I need to find a way to move in Z all the 3 units. Thanks for the answer.

avatar image Vishu2708 · Sep 19, 2021 at 09:03 AM 0
Share

heyy godoy! can i know how u solved that issue? because i m facing with the same issue too

1 Reply

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

Answer by FlaSh-G · May 08, 2018 at 09:24 PM

Transform.Translate, if not told otherwise, translates the object along the local axes. Imagine your cube had a face drawn on the front side. transform.Translate(Vector3.forward); will always move in the direction of that face, no matter where that face points at. So your code does move 3 meters, but not 3 meters in the Z direction. Since you rotated it, the cube will move a bit along the Y axis too.

If you want the cube to move along the Z axis no matter where it's pointing at, you have to specify the optional Parameter for Transform.Translate.

 transform.Translate(Vector3.forward * 3f, Space.World);
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 godoy · May 08, 2018 at 11:28 PM 0
Share

Thank you, I see the "problem" now, my mistake. Now I need to find a way to put the gameobject 3 units away in z-axis. I'm going to try the commutative transformations to solve this.

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

88 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

Related Questions

Moving and rotating a ball 0 Answers

How to rotate an object around its 3 axis and translate it on 2 world axis so it looks like its a dice rolling? 3 Answers

Mesh has rotation/translation when imported 1 Answer

Why does transform.Rotate( x, y, z ) work correctly but transform.Rotate( Vector3.forward ) does not? 1 Answer

Camera rotation around player while following. 6 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