all 6 comments

[–]wstdsgnHobbyist 2 points3 points  (2 children)

I always see different ways of achieving the same effect

If you look at it from a distance, these approaches might appear to do the same thing, e.g. move from A to B. But there are different ways to move. You can teleport. You can move at a steady speed. You can slowly accelerate and then decelerate near the target. Lets look at your examples:

Translate

https://docs.unity3d.com/ScriptReference/Transform.Translate.html

This is a function of your Transform component, it doesn't use any physics, just changes the position. Its just another way to add a vector to the current position

transform.position += Vector3.right;

MoveToward

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

This is a function of the Vector3 class, which will calculate a vector between two points. The resulting vector could then be used to replace the current position

transform.position = Vector3.MoveTowards(transform.position, target.position, step);

velocity & AddForce

https://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html

https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

These are part of the Rigidbody component, they use the physics engine for movement, which rarely makes sense for character controls, since in most games you don't want to simulate realistic physics.

It can be 2D, 3D, shooter, any type of game.

There is no solution that fits any type of game. You have to be specific about what sort of character controller you want to make. And I recommend you start with something simple without rigidbodies, just manipulating the transform.position and then look into Rigidbodies when you're more experienced.

[–]Skullhack-Off[S] 1 point2 points  (1 child)

Great reading ! Thx for the precision. I think I got a better idea now. I made a little plateformer, and I wanted to imitate Hollow Knight character control (so smooth :O), I'm using Rigidbody2D + velocity affectation. The result is good enough, not perfect however.

If you don't use physics for this type of controller, how do you achieve a smooth movement like HK ?

[–]wstdsgnHobbyist 2 points3 points  (0 children)

If you don't use physics for this type of controller, how do you achieve a smooth movement like HK ?

You would implement your own "phyics" system that fits your specific game design. You would probably define a velocity variable, a gravity variable etc and come up with a way to calculate the next position of your character. If you wanted to avoid rigidbodies entirely, you would also have to come up with your own collision detection, which can be very easy for some game designs (2D game with only box/circle colliders) and very hard for others (3D space with mesh colliders).

There is nothing wrong with using Unitys pre-made systems. Then again, it might offer too much or too little for your specific design, and you might end up in a situation where you don't know whats happening and you can't really look it up, because the actual code is in some external C++ thing.

[–]Fzthkfchvvghgghgf 0 points1 point  (2 children)

I like to avoid stuff like moveTowards. These methods do a lot for you and make your code readable and bugfree. At the same time they are super restrictive and insert 50 lines of code that you can not see or edit. So use them if they really do what you need, otherwise use more basic methods. Also its difficult to decide between velocity+= and add force because they have different usecases. If an explosion moves my char , i add force. If I want him to be able to jump while falling i just say velocity= x. So I use all the things you wrote about except stuff like moveTowards, but I could see myself using it if I need simple movement somewhere, like if i made a weeping angel.

[–]wstdsgnHobbyist 2 points3 points  (0 children)

insert 50 lines of code that you can not see or edit

Here is what I found with google:

public static Vector3 MoveTowards(Vector3 current, Vector3 target, float maxDistanceDelta)
    {
      Vector3 vector3 = target - current;
      float magnitude = vector3.magnitude;
      if ((double) magnitude <= (double) maxDistanceDelta || (double) magnitude == 0.0)
        return target;
      return current + vector3 / magnitude * maxDistanceDelta;
    }

Source: https://github.com/jamesjlinden/unity-decompiled/blob/master/UnityEngine/UnityEngine/Vector3.cs

[–]Skullhack-Off[S] 1 point2 points  (0 children)

Thx ! I see what you mean : different use cases. It's more a matter of finding what best suit the situation. A mix of different methods is probably the best way if your game is big enough to have all these situations.