Search Unity

Player controller Unity 2D Character script not working with Surface Effector 2D

Discussion in '2D' started by shanefolden, Sep 22, 2020.

  1. shanefolden

    shanefolden

    Joined:
    Sep 6, 2020
    Posts:
    3
    Hey, all. I am manually setting my movement for my 2d character. The issue is that my player will only move when I give it input, this means all outside forces (specifically surface effector 2D) have no effect on my player. I cannot figure out how to fix, can somebody please offer advice? The problem is that the code is setting my Rigidbody2D velocity directly and I am not educated enough in C# to figure out the solution.

    private Rigidbody2D rb;
    rb.velocity = new Vector2(movementSpeed * movementInputDirection, rb.velocity.y);

    I uploaded my player movement file as well to see the full script, any help is very appreciated.
     

    Attached Files:

  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,473
    There's nothing that can be done here if the controller explicitly sets velocity. Effectors (like all internal physics components) use forces. Those forces during the simulation modify the velocity but then this controller is just stomping over that.

    Your code is always calling "ApplyMovement" so even when "movementInputDirection" is zero so you end up setting the X component of the velocity to zero. In cases like this, it's important to figure out how to debug code as this would be trivial to find either by placing a breakpoint in the code and looking at the value or using "Debug.Log()" to output it to the console.

    At best you can check if there's no movement and don't call it but know that when there is movement, the effector ultimately changing the velocity will have no effect on the X component of the velocity because you stomp over it.

    Hope that helps.
     
    shanefolden likes this.
  3. GreatGardna

    GreatGardna

    Joined:
    Jan 9, 2020
    Posts:
    14
    Hello Melv, I've solved it by creating a bool (isConveyor = true) so that the character slides just like in a conveyor belt. Problem is the physics interaction isn't working quite well, moving against the direction isn't decreasing the speed and moving along the direction isn't increasing.

    Is there a way to solve the problem? thanks in advance.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,473
    What I said previously stills stands in that the effector is adding forces that change the velocity durign simulation but you're just stomping over the velocity directly setting it to what you want. Setting velocity directly gives you exact control over the direction/speed your character moves but it also stomps over anything that's using forces/impulses to adjust the velocity such as gravity, collision-response, effectors etc.

    This is why using Rigidbody2D.AddForce is used so that you're essentially asking for a change in velocity. The downside to this is that it leads to acceleration and deceleration and you'll have to potentially cap the velocity. If you change direction, simply adding a force in the opposite direction means you'll slow down first in the direction you're moving before you eventually start moving in the direction you want. This is why when using AddForce, devs will detect a change in direction, set the velocity in the direction to zero then start adding force if the velocity isn't already at the limit you want your character to move.