Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Thorcek · Jan 23 at 06:30 PM · collisionrigidbodyfixedupdate

Position and collider seem to incorrectly shift when moving object

Hey guys, I'm pretty new to Unity and I'm currently trying to implement small algorithms to move objects around just for fun.


I've read a little about how and when to use the Update or the FixedUpdate loop, or to move a gameobject's rigidbody with MovePosition() instead of using Transform.Translate().


I'm currently trying to create some moving platforms to interact with my player, but I've been bumping into a weird problem recently. I cannot exactly pinpoint its exact source, but it seems that, when I add speed to the platform, its position seems to shift in the opposite direction in the editor while moving.


For reference, here's a top down view of my moving platform heading to the right with a speed of 1, where I've drawn a line from its center to its top right back corner. alt text


Here's the same platform, but with a speed of 15. alt text


As you can see, the theoretical position of my platform seems to move as the speed increases, which causes problems with collision detection directly in the editor and OnCollisionEnter/Stay/Exit methods. At that speed, because of the shift, if I place a gameobject a couple of pixels to the left of the platform, a collision occurs and the object follows the platform's movements, even if the two are not visually colliding. The opposite seems to happen on the other side of the platform, where a collision should occur, but does not.


I've noticed that if I replace the FixedUpdate loop by an Update loop, the line seems to be correctly aligned, no matter the speed, but I've read that any physics related operation should preferably be executed in the FixedUpdate loop. Even so, switching to an Update loop does not fix the collision detection problem.


The platform is a simple gameobject with a kinematic rigidbody (where I froze the rotation on each axis) and a box collider wrapping the whole object.


Here's the code I'm using for my moving platform.

     public class MovingPlatform : MonoBehaviour
     {
         [SerializeField]
         private List<Vector3> _positions;
         [SerializeField]
         private float _speed;
 
         private int index;
         private bool reverse;
         private Rigidbody _rigidbody;
         private Dictionary<GameObject, Rigidbody> _attachedGameObjects;
 
         public void Awake()
         {
             _attachedGameObjects = new Dictionary<GameObject, Rigidbody>();
             _rigidbody = gameObject.GetComponent<Rigidbody>();
         }
 
         public void Start()
         {
             _positions.Insert(0, gameObject.transform.position);
         }
 
         public void FixedUpdate()
         {
             if (gameObject.transform.position == _positions[index])
             {
                 ChooseNextReachPoint();
             }
 
             float step = _speed * Time.fixedDeltaTime;
             Vector3 newPosition = Vector3.MoveTowards(gameObject.transform.position, _positions[index], step);
             Vector3 moveValue = newPosition - gameObject.transform.position;
 
             foreach (KeyValuePair<GameObject, Rigidbody> attachedGameObject in _attachedGameObjects)
             {
                 Vector3 newAttachedGameObjectPosition = attachedGameObject.Key.transform.position + moveValue;
                 attachedGameObject.Value.MovePosition(newAttachedGameObjectPosition);
             }
 
             _rigidbody.MovePosition(newPosition);
 
             Debug.DrawLine(gameObject.transform.position, gameObject.transform.position + transform.localScale / 2, 
                 Color.red, Time.fixedDeltaTime, false);
         }
 
         private void ChooseNextReachPoint()
         {
             if (index == 0)
             {
                 reverse = false;
             }
             else if (index + 1 == _positions.Count)
             {
                 reverse = true;
             }
             index += reverse ? -1 : 1;
         }
 
         private void OnCollisionStay(Collision collision)
         {
             //TODO: Only add if on top of platform
             if (!_attachedGameObjects.ContainsKey(collision.gameObject))
             {
                 _attachedGameObjects.Add(collision.gameObject, collision.rigidbody);
             }
         }
 
         private void OnCollisionExit(Collision collision)
         {
             _attachedGameObjects.Remove(collision.gameObject);
         }
     }


Is there anything wrong with my understanding of the FixedUpdate vs Update concept, my code, or anything else?


Thanks!

unity-platform-speed-15.png (25.3 kB)
unity-platform-speed-1.png (12.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

276 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Detecting collisions on an invisible grid 0 Answers

Unity 5: AddForce Increases power when already being pushed towards a collider. How to make stop? 1 Answer

Moving a cube with RigidBody. MovePostition, it randomly stops moving 0 Answers

How to make rigidbody not effect movement while still using it for collisions? 0 Answers

Help with colliders on concave shapes with rigid bodies 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges