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 /
avatar image
0
Question by hblack · Oct 19, 2018 at 04:39 PM · rotationdirectiontranslate

Rotate prefab to face movement directio

I need help with prefab rotation

I have script attached to gameobject that spawns prefabs just as intended. void Update() {

         if (Time.time > nextSpawn)
             {
                 Spawn();
             }
     }
 
         public void Spawn()
            {
             nextSpawn = Time.time + spawnRate;
             randSpawnX = Random.Range(leftBorder, rightBorder);
             whereToSpawn = new Vector2(randSpawnX, transform.position.y);
             SpawnRandomBug();
             }
 
         public void SpawnRandomBug()
             {
             randomInteger = Random.Range(0, bugs.Length);
             Instantiate(bugs[randomInteger], whereToSpawn, Quaternion.identity);
             }
     }

I also have script atached to prefabs that move that as i like

private void Start() { randX = Random.Range(-2.7f, 2.7f); randY = Random.Range(-4.9f, 4.9f); moveVector = new Vector3(randX, randY, 0f); }

     void Update () {
         MoveTheBug();
     }
 
     public void MoveTheBug()
     {
         transform.Translate(moveVector * moveSpeed * Time.deltaTime);
     }

But I just can't make prefabs face direction of movement. I tried several things found on this forum but I just can't make it work.

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

2 Replies

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

Answer by mjdb3d · Oct 19, 2018 at 05:07 PM

link textTry this

      void Update () {
          MoveTheBug();
      }
  
      public void MoveTheBug()
      {
               transform.Translate(Vector3.up * moveSpeed * Time.deltaTime);

               float angle = Quaternion.FromToRotation(Vector3.up, moveVector).eulerAngles.z;

               transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
      }



rotateprefabtofacemovementdirection.zip (3.5 kB)
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 hblack · Oct 20, 2018 at 05:56 AM 0
Share

Thank you! This solved it for me!

avatar image
0

Answer by Glurth · Oct 19, 2018 at 05:05 PM

 transform.Translate(moveVector * moveSpeed * Time.deltaTime);

  //I assume moveVector is a local-space vector
 Vector3 worldMoveDir = transform.TransformDirection(moveVector.normalized);
 transform.LookAt(worldMoveDir);

Edit: ah, for 2d on the XY plane, you would need to add, the up parameter.

 transform.LookAt(worldMoveDir, new Vector3(0,0,1) );

It doesn't translate: oops, my mistake- once pointing in the right "world-space" direction- your translate function, which takes a local-space vector, should always be "forward".

 transform.Translate(new Vector3(1,0,0) * moveSpeed * 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 hblack · Oct 19, 2018 at 06:07 PM 0
Share

Unfortunately, neither of those worked.

@Glurth - your version creates spinning of prefabs around y axis and they do translate to target direction any more. Rays show direction where they should translate. facing direction

@mjdb3d - you version creates similar situation with little more rotation. facing direction

vector-error.png (21.1 kB)
vector-error2.png (13.9 kB)

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

127 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

Related Questions

transform.Translate but Transform has Rotation!=0 1 Answer

Rotation to go from one direction to another? 0 Answers

Rotation of character resets when joystick is released 3 Answers

why my wander script makes my enemies move badly? 1 Answer

Checking vector3 gameObject is about to move to (pathfinding collision detection) 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