Search Unity

Question Move toward specific direction in 2D

Discussion in 'Scripting' started by cheapcpps, May 17, 2022.

  1. cheapcpps

    cheapcpps

    Joined:
    Jul 15, 2020
    Posts:
    53
    Hi. I'm looking to make a 2d Object move towards a direction (not the direction it's facing, any direction), without a rigidbody, however, nothing I found online works after maybe an hour of searching, I also noticed commonly, for the direction variable, people use Vector2D, which is not what I want, I want of float of the z direction, (which is the only common direction axis used in 2D).

    Everything online either didnt work, or one managed to use some curved motion, but kept going even after it the direction changed. So I created my own using some simple math logic.

    Code (CSharp):
    1. transform.position += new Vector3((float) d * Mathf.Sin(dir), (float)d * Mathf.Cos(dir));
    This is the closest I've got, and the closest result that some code caused, however, there is still an issue with it. For some reason it moves the opposite way of the direction, I tried subtracting from the position instead of adding, didnt work, adding to the direction, also didn't work. Instead of moving in the direction this code practically pushes it away.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Given:

    -
    Vector2.up
    points up (0,1)

    -
    Angle
    is a rotation around Z in degrees

    Movement direction is:

    Code (csharp):
    1. Vector2 movement = Quaternion.Euler( 0, 0, Angle) * Vector2.up;
    2.  
    3. transform.position += movement * (Time.deltaTime * 10);
    Make angle public so you can change it.

    Put the above in Update() on a cube and press play.
     
  3. cheapcpps

    cheapcpps

    Joined:
    Jul 15, 2020
    Posts:
    53
    Same issue as I had before. My sprite is rotating towards the mouse, however, it's now moving AROUND the mouse cursor's world space position, rather than doing what I want, which is moving in that direction.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Here's how to start debugging:

    You must find a way to get the information you need in order to reason about what the problem is.

    What is often happening in these cases is one of the following:

    - the code you think is executing is not actually executing at all
    - the code is executing far EARLIER or LATER than you think
    - the code is executing far LESS OFTEN than you think
    - the code is executing far MORE OFTEN than you think
    - the code is executing on another GameObject than you think it is
    - you're getting an error or warning and you haven't noticed it in the console window

    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?
    - are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

    Knowing this information will help you reason about the behavior you are seeing.

    If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

    You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

    You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: https://forum.unity.com/threads/how-to-capturing-device-logs-on-ios.529920/ or this answer for Android: https://forum.unity.com/threads/how-to-capturing-device-logs-on-android.528680/

    Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
  5. cheapcpps

    cheapcpps

    Joined:
    Jul 15, 2020
    Posts:
    53
    Not useful at all. You basically gave me code, it didn't work, and then copy and pasted a debugging message that doesn't really apply to my situation, and told me to debug it.