Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Rotate and move

Discussion in '2D' started by DrDress, Mar 13, 2020.

  1. DrDress

    DrDress

    Joined:
    Sep 11, 2019
    Posts:
    30
    I want to move units around the map like tanks. So I get a target as a Vector3; the unit then turn in increments until pointing to the target. It then starting towards the target. I am confused about angles, quaternion, local- and global coordinates. I can proberly get it working using my own vector- and linear algebra, but I would prefer to learn Unitys logic. So any ideas?
     
  2. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @DrDress

    Instead of starting a guessing game - why not just post your code. Although it sounds like you don't already have any code... so it is a bit hard help.

    But I would simply do this each tick / frame:
    - Move forward along transform forward, using your move speed.
    - Turn towards target direction using your tank rotation speed.
     
  3. DrDress

    DrDress

    Joined:
    Sep 11, 2019
    Posts:
    30
    You are right that I don't have any code yet, since I wanted to implement the "correct" version. But you seem to have grasped exactly what I want do do. I guess the psudo code would look something like this:

    Code (CSharp):
    1.  
    2.     void SetDirection(Vector3 target)
    3.     {
    4.         // Do something to figure out how to turn
    5.     }
    6.  
    7.     void Update()
    8.     {
    9.         if (facingTarget???)
    10.         {
    11.             transform.Translate(0.1f * Vector3.up);
    12.         }
    13.        else
    14.        {
    15.             transform.Rotate(???);
    16.         }
    17.     }
    18.  
    But I am not sure how the rotation part would go
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @DrDress

    I think you should read a bit about what is available in Unity API - most helpful methods are found in both Vector3 and Quaternion class - IMHO. See for example:

    https://docs.unity3d.com/ScriptReference/Vector3.MoveTowards.html
    https://docs.unity3d.com/ScriptReference/Quaternion.RotateTowards.html

    You didn't either specify on which plane you move; using 3D directions (z-axis is forward) is usually easiest for moving and turning (because Unity thinks users always want z-axis to be forward...), but with a one or two additional steps you can do the same things on 2D (xy) plane.

    You can find good links if you just google for Unity steering behavior, Unity move towards (target), Unity rotate towards.
     
  5. DrDress

    DrDress

    Joined:
    Sep 11, 2019
    Posts:
    30
    Ok. Thanks. That's big help