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.
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.
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.
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:
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.
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?