- Home /
Bumping kinematic objects
I have what seems like a small issue, but I really am having trouble getting it to work and I am not sure why.
I have a few objects on a platform that are all supposed to be bumping into each other. Each one of these objects is a parent with a kinematic rigid body which has 3 children, all with colliders and kinematic rigidbodies of their own. I have a script on one of the children, the main "Body" of these objects, that has a script to be pushed back in the opposite direction of the object that they hit. This is happening with OnTriggerEnter.
For some reason sometimes the object go right through each other, instead of the opposite direction, sometimes they go in some random direction. It works great when only one is moving and bumping into a stationary one, but if they are both going full speed, then that is when things go awry. It is even worse when there are more than 2 on the platform.
Here is the script below:
public class BodyCollision : MonoBehaviour {
private bool hit = false;
private bool moving = false;
public float distance;
float time = 1.0f;
Vector3 dir;
GameObject parent;
void OnTriggerEnter(Collider col)
{
if(col.gameObject.tag == "Island")
{
Debug.Log("Hello");
}
else if(col.gameObject.tag != "Island")
{
if(hit == false)
{
hit = true;
if(col.gameObject.tag == "Body")
{
Debug.Log("happened");
dir = transform.root.position - col.transform.root.position;
dir.y = 0f;
parent = col.transform.root.gameObject;
if(parent.tag == "Kart")
{
dir = -dir;
parent.GetComponent<BotController>().bumping = true;
}
else if(parent.tag == "Player")
{
parent.GetComponent<PlayerController>().bumping = true;
}
moving = true;
}
hit = false;
}
}
}
void Update()
{
if(moving == true)
{
transform.root.Translate(dir * (Time.deltaTime * (distance/time)));
StartCoroutine(BumpDelay());
}
}
IEnumerator BumpDelay()
{
yield return new WaitForSeconds(0.1f);
moving = false;
}
}
bumping stops the Player and other Bots from moving during the bumps, which is why that is set to 'true' on the bump, but after they are called back to false in their respective control scripts. If it makes a difference, the player is moved with transform.Translate and the Bots are moved with MoveTowards().
I am also using kinematic objects because I want real control of the bumps. I had non-kinematic rigidbodies before with AddForce() on bumps and they would get all tangled up.
I would appreciate any help I can get. Thank you so much.
Answer by cgraf1 · Dec 04, 2018 at 08:54 PM
I realized that I did not normalize the direction of the movement. I have done so since, but it has not led to any better results.
I have also tried changing the OnTriggerEnter to OnCollisionEnter and going to the Physics Manager to enable kinematic-kinematic collisions, but the results were the same.
I would be very grateful if anyone had any other ideas.
Your answer
![](https://koobas.hobune.stream/wayback/20220612183304im_/https://answers.unity.com/themes/thub/images/avi.jpg)