Search Unity

Rigidbody2D Character walking on a CircleCollider2D with a PointEffector2D

Discussion in '2D' started by 94chackal94, Apr 24, 2019.

?

Was my post sufficiently explanatory?

Poll closed May 1, 2019.
  1. Sufficient, I understood the question.

    0 vote(s)
    0.0%
  2. Insufficient, I couldn't understand the question.

    0 vote(s)
    0.0%
Multiple votes are allowed.
  1. 94chackal94

    94chackal94

    Joined:
    Jan 17, 2016
    Posts:
    3
    Hi all, first and foremost thanks for viewing my question.

    I am trying to make a football player character 2D run along a planet 2D and collide with the incoming ball. There is an issue though. I add Rigidbody2D and Collider2D to my character while I add PointEffector2D and 2 Collider2D for my planet, 1 for collisions and 1 for gravity. My character can and does stand on its feet pointing towards the earth at all times. However as I take Input from the character and apply this input with either;

    Code (CSharp):
    1. Rigidbody2D rigidBody = GetComponent<RigidBody2D>;
    2. rigidBody.velocity = rigidBody.velocity + (transform.right) * Time.deltaTime;
    or

    Code (CSharp):
    1. Rigidbody2D rigidBody = GetComponent<RigidBody2D>;
    2. rigidBody.AddForce(transform.right * Time.deltaTime);
    I am getting a behaviour such realistic that my character gets into a sub-orbital trajectory followed by an escape trajectory if Input is given for a sufficient time. First if the input is weak and slow enough the character just rotates around the planet, however this speed is limited due to the fact that when a certain angular velocity is achieved by the character he starts floating away. I would like to limit my run on ground as long as I don't jump. So far, all my implementations created some errors. What are your valuable thoughts on this?

    Thanks.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,807
    If the character is grounded, lock him to the ground every frame. This could be done by clamping the distance the character can be away from the planet if it's perfectly circular, or you could raycast down from the character to the planet surface and set the character's position & rotation to the hit point and surface normal (or the direction between the character and the planet).