• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

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

3D Top down movement. Move towards the mouse position. 1 Answer

Bullet follows player mouvement when It instintiates. 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

How to auto rotate a object, during it moving in a path. 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges