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
1
Question by wenson16 · Jan 03, 2017 at 10:02 PM · movement2d rotationtop down shooterturns

Face target first then move forward to it - Top Down 2D

So I have a 2D top down scene with X and Y axis, what I want to happen to my object is to turn to its target first then eventually move forward to it. I have managed to make it turn to face its target but it doesn't go forward. Here's my code.

 void Update ()
     {
         if (userInput.currentWayPoint != null) {
             isRotating = true;
             if (isRotating)
                 TurnToTarget (userInput.currentWayPoint);
             else if (isMoving)
                 Move (userInput.currentWayPoint);
         } else {
             isRotating = false;
         }
     }
 
 void TurnToTarget (Vector3 target) {
         Vector3 diff = target - transform.position;
         diff.Normalize ();
         rotZ = Mathf.Atan2 (diff.y, diff.x) * Mathf.Rad2Deg;
         rotationDirection = Quaternion.Euler (0, 0, rotZ - scatterForm);
         transform.rotation = Quaternion.RotateTowards (transform.rotation, rotationDirection, rotateSpeed * Time.deltaTime);
         isRotating = false;
         isMoving = true;
     }
 
 void Move (Vector3 target) {
         Vector3 diff = target - transform.position;
         transform.position = Vector3.MoveTowards(transform.position, diff, Time.deltaTime * speed);
     }

currentWayPoint is handling the position of the mouse which serves as my target

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

Answer by Creeper_Math · Jan 03, 2017 at 11:08 PM

You have very interesting "if then" statements.. here's what you have....

 if (userInput.currentWayPoint != null) {
              isRotating = true;
              if (isRotating)
                  TurnToTarget (userInput.currentWayPoint);
              else if (isMoving)
                  Move (userInput.currentWayPoint);
          } else {
              isRotating = false;
          }

First of all... you're setting "isRotating" to true and immediately testing to see if it's true... Which can be just set to true and removed... The specific lines that you had are below

               isRotating = true;
               if (isRotating)

Then you're saying that if it's not true (which will never happen), then you execute your moving script (which therefore will never occur) The specific lines are below

                   else if (isMoving)
                   Move (userInput.currentWayPoint);

Therefore the moving is not working...

A fix for this would be the following

 if (userInput.currentWayPoint != null) {
              isRotating = true;
              TurnToTarget (userInput.currentWayPoint);
              if (isMoving) {
                  Move (userInput.currentWayPoint);
              }
          } else {
              isRotating = false;
          }

Looking further, this would still not work, as your rotating script is always saying that's it's finished every update, even if it's the first rotation update it had. This in particular includes the isRotating = false; and isMoving = true; .

An easy fix would be to replace your rotating script with

 void TurnToTarget (Vector3 target) {
          Vector3 diff = target - transform.position;
          diff.Normalize ();
          rotZ = Mathf.Atan2 (diff.y, diff.x) * Mathf.Rad2Deg;
          rotationDirection = Quaternion.Euler (0, 0, rotZ - scatterForm);
          transform.rotation = Quaternion.RotateTowards (transform.rotation, rotationDirection, rotateSpeed * Time.deltaTime);
          if (transform.rotation == rotationDirection) {
                 isRotating = false;
                 isMoving = true;
          }
      }

Where it will now only say that isRotating is false and set "isMoving" to true when it is facing the target

Hopefully this helps you! If you want it so that it will start moving a bit quicker after it rotates then I can provide you with another replacement for the final script that I have here!

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Player won't look in direction and travel at the same time? 2 Answers

Bullet follows player mouvement when It instintiates. 1 Answer

Rotate to where the player is moving 1 Answer

How can I rotate an object towards a collision normal in 2D? 0 Answers

How to auto rotate a object, during it moving in a path. 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