- Home /
Rigidbodys on top of a moving object are not moving with said object.
I have an object that I am moving with
transform.position = new Vector3(Mathf.PingPong(Time.time*sliderSpeed,max-min)+min, transform.position.y, transform.position.z);
It Movies back and forth from one point to the next. The problem I'm having is that when I place a rigidbody on top of that moving object the rigidbody doesn't move with it, it does, however, use the moving objects collision and can be pushed by it. The project I'm working on needs whatever object is on top of it to move with it.
Does anyone know of any ways around it, or if I'm doing something wrong with pingpong.
Both objects are 3d, with mesh collider on one and box collider on the other, have tried making both use box colliders, etc.
Cheers.
Answer by metalted · Jun 01, 2019 at 08:39 PM
The position calculation you are using is fine and the PingPong is working correctly. Its just the way you are moving the object. When you directly set a transform.position it will calculate collisions but it will skip the physics engine. That is why your top block isn't moving, because it isn't feeling any forces. It just slides in the same spot.
For this to work, you will need to add a rigidbody to your platform and set it to kinematic. Then instead of setting the position directly you use Rigidbody.MovePosition(); You can reuse the Vector3 you created:
private Rigidbody rb;
public void Start()
{
rb = GetComponent<Rigidbody>();
}
public void Update()
{
rb.MovePosition(new Vector3(Mathf.PingPong(Time.time*sliderSpeed,max-min)+min, transform.position.y, transform.position.z));
}
Now the object will be moved with forces and will interact with other objects. When you are working with rigidbodies, always use MovePosition instead of using position directly, unless ofcourse you really need it for some reason.
This is exactly what I need! I did have a rigidbody on it and set to kinematic but I was still using the original transform.postion. the $$anonymous$$ovePosition was what I was missing.
Thanks so much!
Another thought here is to parent the moving object to the one you want to move with it.
Hi, thanks for the reply, I did have this thought however it just isn't viable for the desired effects I want, it would just make things too complicated in this case.
Fair enough, I have just had wacky behaviour when I didn’t do so in the past so I thought I would mention it
Answer by NoCandyIncluded · Jun 01, 2019 at 04:03 AM
Try increasing the angular drag in the rigidbody component, if that doesnt work too well, try increasing the drag, or a bit of both. You may need to set either drag settings to something high, so try a variety of values.
I have tried both with no different results. The project is a coin pusher game, I'm spawning coins on top of a sliding object if that helps anyone get a better idea of what I'm trying to do. The coins land on top of the object and stay in place until the sliding object moves from underneath them. What I want to happen is for the coins to stay on top and get pushed off by an additional collision box towards the back.
Your answer
![](https://koobas.hobune.stream/wayback/20220612202427im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to call OnTriggerEnter once 5 Answers
Button Enabling/Disabling using Collision Triggers? 1 Answer
How to trigger an event if the collided object has the tag “Player” 3 Answers