playMaker

Author Topic: Best way to move 2d rb around?  (Read 482 times)

Krillan87

  • Beta Group
  • Full Member
  • *
  • Posts: 185
    • View Profile
Best way to move 2d rb around?
« on: September 13, 2019, 07:54:30 AM »
Hi,

So I have a general question about moving 2d RB around. In my project right now I have like three different ways to do it and I can't find the "correct" method.

I have 2 reoccurring events that happens a lot in my project.

1) I need to move a RB along side a path. I tried a lot of different ways and I'am not really satisfied with anyone. Right now I just use "Move object" (do not even know if it is suited for 2D to begun with). For some reason I need to set the time-variable on the action and this makes the speed of the rb dependent of the travel distance. I then use a finish event that is "go to next waypoint".

What I really want here is just an action that moves a 2d rb with constant speed intill ill reaches a destination and then go the the next event.

2) basically I would like to use a 2d version of the "move towards" that correctly interacts with physics. It seems like "move towards" and "set velocity 2d" basically just fix the velocity at a certain number meaning a very small object with a low velocity can still push around a huge object. Right now I use "add force" instead but that is a lot more issues and complications. (I need to apply the force correctly and lock constraints etc etc).

Does anyone have any inputs hon a good practices here? I have tried some "move towards advanced" and other ecosystem actions but no one seems to be just right.

Thanks,
Christian

Thore

  • Sr. Member
  • ****
  • Posts: 480
    • View Profile
Re: Best way to move 2d rb around?
« Reply #1 on: September 13, 2019, 08:43:38 AM »
You can switch the Rigidbody also to kinematic.

if enabled, Forces, collisions or joints will not affect the rigidbody anymore

You can also not use a collider, or set it onto a layer that ignores certain other layers. Or you can set the mass high, and that way, it's not pushed around. Unity doesn't "know" how big an object is, unless you tell it the mass (or switch Auto Mass on).

I'd go with Set Velocity 2D, but it totally depends on what you overall try to achieve.

Krillan87

  • Beta Group
  • Full Member
  • *
  • Posts: 185
    • View Profile
Re: Best way to move 2d rb around?
« Reply #2 on: September 13, 2019, 09:12:44 AM »
You can switch the Rigidbody also to kinematic.

if enabled, Forces, collisions or joints will not affect the rigidbody anymore

You can also not use a collider, or set it onto a layer that ignores certain other layers. Or you can set the mass high, and that way, it's not pushed around. Unity doesn't "know" how big an object is, unless you tell it the mass (or switch Auto Mass on).

I'd go with Set Velocity 2D, but it totally depends on what you overall try to achieve.

Thanks for your reply!
I do not really understand what of my questions your answer was aimed to :P.

I can add some more information regarding my setup: on  1) is one of the characters in the game. I cannot change the rb typ because that will mess up a lot of other interaction with the character and physics.

I need the objects to have collides in other instances so I need to keep the collides (to detect triggers and collision).

2) one way is to use layers in some ways. This has some other issues. mainly they are already in a specific layer of other purposes, so I rather find a easier way.

Regarding the mass; if you set a velocity on a object it doesn't matter what mass it has. Unity will force the object to have said velocity. So i object with 5 in mass and 2 in velocity will still push around a object with 1000 in mass, (not very fast but it will still push it).

(Sorry if I sound ungrateful, that is defiantly not my point, English is not my first language so I may sound harsh :P)

Thore

  • Sr. Member
  • ****
  • Posts: 480
    • View Profile
Re: Best way to move 2d rb around?
« Reply #3 on: September 13, 2019, 10:51:36 AM »
I think the issue is currently in your design. You need to find out first what is supposed to happen when two objects collide. For example, you can make an FSM that stops velocity on collision. You also need to sort out when you want the object to participate in physics and when not, and how.