Move toward specific direction in 2D

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.

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.

Given:

  • Vector2.up points up (0,1)

  • Angle is a rotation around Z in degrees

Movement direction is:

Vector2 movement = Quaternion.Euler( 0, 0, Angle) * Vector2.up;

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.

1 Like

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.

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://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

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://discussions.unity.com/t/839300/3

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.

Did you ever find a solution to this? I essentially have the same issue

Please don’t necro-post. If you have a new question, make a new post. It’s FREE!!

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379