Search Unity

Should I move a character using physics?

Discussion in '2D' started by Abdo023, Dec 18, 2017.

  1. Abdo023

    Abdo023

    Joined:
    Dec 18, 2017
    Posts:
    64
    Hello, I'm new to unity. I was wondering what is the best way to move a character in a 2d game whether it's platformer or top-down. I've watched some youtube videos and they all move the character by manipulation it's velocity in the FixedUpdate function. But, as far as I know, moving a character using physics is not a great idea. But, what would be the alternative in unity? and is there some kind of tutorial or asset for that?
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
  3. steg90

    steg90

    Joined:
    Jan 26, 2016
    Posts:
    93
    I just do the following in my gameobject(player) update method:

    float hrz = Input.GetAxis("Horizontal");
    float vrz = Input.GetAxis("Vertical");
    transform.Translate(hrz * Time.deltaTime, vrz * Time.deltaTime, 0);
     
  4. Abdo023

    Abdo023

    Joined:
    Dec 18, 2017
    Posts:
    64
    And your character is Kinematic?
     
  5. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,536
    If you use a Rigidbody then it is best to use Rigidbody.MovePosition to move it. Using Transform.Translate will simply teleport it to a new position regardless of physics and the next time physics reads that rigidbody it's going to be pretty confused and possibly have a difficult (impossible) time trying to resolve any collisions it might have created whereas MovePosition will sort these out first and try to move it as reasonably as possible without exploding the whole simulation.

    So it's really one or the other, either make a controller using a rigidbody and use physics or do it with no rigidbody and use transform.
     
    nhokbon123 and kavanavak like this.
  6. steg90

    steg90

    Joined:
    Jan 26, 2016
    Posts:
    93
    My character has no physics attached to it. We are just moving its position.
     
  7. Abdo023

    Abdo023

    Joined:
    Dec 18, 2017
    Posts:
    64
    But without a rigidbody, how can it detect collisions?
     
  8. steg90

    steg90

    Joined:
    Jan 26, 2016
    Posts:
    93
    You would need to do roll your own collision detection without rigidbody, but you can still have a rigidbody.
     
  9. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,536
    Depends on how much control you want. TBH creating a good controller either way is not particularly simple. Using a rigidbody will afford you some luxuries, but now you really don't have much good reason to use Translate and would be better off with MovePosition. So... It's basically one way or the other when you think about it.
     
  10. AssembledVS

    AssembledVS

    Joined:
    Feb 23, 2014
    Posts:
    248
    Why is MovePosition better than changing velocity directly or using AddForce? Is it "cleaner"? More precise? Simpler?
     
  11. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,536
    Simpler. Adding force manually and taking ownership of velocity works fine but is more work.