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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Changing collider position results in different physics. Why?

Discussion in 'Editor & General Support' started by Garmichael, Feb 19, 2014.

  1. Garmichael

    Garmichael

    Joined:
    Mar 17, 2013
    Posts:
    17
    I don't understand why this is happening. An explanation would help greatly.

    I have two objects, Player and Player2. I wrote some code to move these objects. They both have capsule colliders and rigidbodies. Player2 is an exact copy of Player except I set the capsule collider center y position to 1 instead of 0.

    To move the objects, I do the following code:
    Code (csharp):
    1.  
    2. float movemax = 8;
    3. //Get current touch position, check distance from start touch position.
    4. //Update start position if cur touch is too far from it.
    5. if(t.position.x - movecenter.x > movefromcenter){
    6.     movecenter.x = t.position.x - movefromcenter;
    7. }
    8. if(t.position.x - movecenter.x < -movefromcenter){
    9.     movecenter.x = t.position.x + movefromcenter;
    10. }
    11. if(t.position.y - movecenter.y > movefromcenter){
    12.     movecenter.y = t.position.y - movefromcenter;
    13. }
    14. if(t.position.y - movecenter.y < -movefromcenter){
    15.     movecenter.y = t.position.y + movefromcenter;
    16. }
    17.  
    18. Vector2 moveamount = new Vector2((t.position.x - movecenter.x)/movefromcenter, (t.position.y - movecenter.y)/movefromcenter);
    19.  
    20. //Apply Velocity to both Players based on distance from touch start and touch cur
    21. Player.rigidbody.velocity = new Vector3(movemax * moveamount.x,Player.rigidbody.velocity.y,movemax * moveamount.y);
    22. Player2.rigidbody.velocity = new Vector3(movemax * moveamount.x,Player2.rigidbody.velocity.y,movemax * moveamount.y);
    23.  
    24.            
    25.            
    When both objects have the same collider position, they both move in unison. However, when I change the position of Player2's collider center.y to 1, it moves slower, and when I set it to -1, it moves faster.

    If I'm not clear enough in my description, I can supply screenshots.

    My question here is: why does the object move differently if I adjust the collider's position?
     
  2. Garmichael

    Garmichael

    Joined:
    Mar 17, 2013
    Posts:
    17