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
0
Question by esitoinatteso · Mar 12, 2015 at 08:30 PM · rotationmovementtransformtranslatestraight

transform.Translate but Transform has Rotation!=0

Hi there!

I was testing enemies movements in my wannabe game, and I've noticed a behaviour that I really don't like.

Basically, I got a simple translation function that should move the enemies to a given target Vector 3. Skipping useless details, here's the function itself:

 //Your regular Translation function
     void Move(Vector3 _target){
 
         //DEBUG
         if(Commander.allStandStill){
 
             //Do nuffin!
         }
         else{
             //First we define the direction - target is assumed as specified.
             direction= _target - transform.position;
             
             //We normalize it
             direction = direction.normalized;
             
             //We set up a Vector used to move you
             moveVector = direction * speed * Time.deltaTime;
 
             //We scale this so that speed 1 is the minimum value to move you
             moveVector *= 12f;
             
             //And then we move you
             //Space.World helps moving decently even with a 25° X rotation
             transform.Translate(moveVector.x,0f,moveVector.z, Space.World);
         }
     }

Odd Things ( number 2 is the focus of this question):

  1. As you can see I have to multiply moveVector by 12f. I don't know why, but if I set the speed <= 11 the enemy doesn't move, while values of 12 or above works. My solution was to multiply the vector by 12f so that a speed of 1 could work ( thus the scale starts at 1 now);

  2. The enemy translates but it doesn't follow a linear direction even though it's supposed to do so. My game is 2.5d so little imprecisions of floats for x/z axes are fine, but these curves are obviously more than just a rushed rounding.

I've managed to track down a bit what I believe is the source of these curves: the object's rotation.

All the object's in my game have 25° of rotation on the X axis. That's my 2 cents in tweaking the perspective a little bit so the game seems deeper ( right now the camera is not ortho, and these 25 degrees really show off).

As a matter of fact, I've already tried the posted code on an enemy with default rotation ( 0) and everything went as expected: no curves or other oddities.

Question is: What would I need to fix those curves and make translation straight with a rotated object?

If you need to know more about my current set up, please let me know!

P.S.: If you think .Translate() is not the best method to use, feel free to point me toward other methods! Thanks for your time

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

Answer by d2 · Mar 12, 2015 at 09:31 PM

you could change move with MoveToowards instead of translate, something like this:

transform.position = Vector3.MoveTowards(transform.position, _target, speed * Time.deltaTime);

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 esitoinatteso · Mar 13, 2015 at 05:34 PM 0
Share

Hi d2! Thanks for you suggestion, I've tried but the result is the same; it seems that Unity handles the .$$anonymous$$oveTowards() under the hood, so you only have one line of code on screen, but does the same things I was doing with my code. Still, it's more pleasant to the eye, so thanks for pointing that out! ( is it also faster?)

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

21 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

Related Questions

Rotatating a character 2 Answers

Moving multiple transforms from an array in a single script 0 Answers

How do I get GameObjects to look at me? 2 Answers

Moving an object 1 Answer

Player movement adding sprint, issues with rotation 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