Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Issue after adding rigidbox to 2d gameobjects

Discussion in '2D' started by mexicanlefty, Oct 17, 2019.

  1. mexicanlefty

    mexicanlefty

    Joined:
    Oct 17, 2019
    Posts:
    7
    So im starting with unity, im doing a tutorial (Ruby's adventure: 2d) And after adding the collision physics on the gameobjects with the Rigidbody2D function, the program started to malfunction: the collision is not where the gameobject is drawn and the size of the character changes (gets bigger) when going into play mode. So i have no idea how to solve this and i have followed the tutorial step by step.
    tI havehe latest unity version installed, dont know if thats an issue.
     
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    If you would post a screenshot of your object's components in the inspector, or a video of what you're seeing, it would help inform us and possibly spot your mistake. Otherwise we're just speculating.
     
  3. mexicanlefty

    mexicanlefty

    Joined:
    Oct 17, 2019
    Posts:
    7
    I did another thread that has more detail https://forum.unity.com/threads/my-colliders-are-not-where-the-gameobject-is-drawn.762608/

    Otherwise the images are the following:

    Editor mode: https://imgur.com/YYfIGBU
    Play mode: https://imgur.com/tR4YSMw
    Colliders (red is where my character collides and yellow where the collider is supossed to be): https://imgur.com/imPjpTD
     
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    First, use code blocks:
    Code (CSharp):
    1. public class RubyController : MonoBehaviour { Rigidbody2D rigidbody2d;
    2.  
    3. // Start is called before the first frame update
    4. void Start()
    5. {
    6. rigidbody2d = GetComponent<Rigidbody2D>();
    7. }
    8.  
    9. // Update is called once per frame
    10. void Update()
    11. {
    12. float horizontal = Input.GetAxis("Horizontal");
    13. float vertical = Input.GetAxis("Vertical");
    14.  
    15. Vector2 position = rigidbody2d.position;
    16. position.x = position.x + 3.0f* horizontal * Time.deltaTime;
    17. position.y = position.y + 3.0f * vertical * Time.deltaTime;
    18.  
    19. rigidbody2d.MovePosition(position);
    20. }
    21.  
    22. }
    Now let's look at your code. Particularly this:
    Code (CSharp):
    1. position.x = position.x + 3.0f* horizontal * Time.deltaTime;
    2. position.y = position.y + 3.0f * vertical * Time.deltaTime;
    This looks to me as you're offsetting your position by adding 3.0f.

    Your horizontal might be more efficient as:
    Code (CSharp):
    1. position.x = position.x * horizontal * 3.0f * Time.deltaTime;
    The reason I think this is because this would be your movement speed. You wouldn't add that value to position.x or it'll move that position over 3.0f which does seem to be what's happening in your diagram.

    Edit:

    Perhaps I used the wrong arithmetic operator in that example, might need to be:
    Code (CSharp):
    1. position.x = position.x + horizontal * 3.0f  * Time.deltaTime;
    So when you really look what's going on it's this:
    Code (CSharp):
    1. position.x = position.x + (horizontal * 3.0f)  * Time.deltaTime;
     
    Last edited: Oct 17, 2019