- Home /
Continuously moving rigidbody
Hello, I'm back again asking a second question for my 2D game, wondering why my rigidbodies are stopping and how to to keep them moving around the field/randomly at all times. Basically, I have a rigidbodies that spawn at a random location and have a vector force applied to them at start to begin the moving motion. These rigidbodies are not kinematic and no gravity is enforced. To keep them within bounds, I have added pairs or box colliders around the borders of the game. One collider in each pair acts as the actual wall/boundary with an attached physic material with a bounce factor of 1 and no friction. The second collider in a pair has a script the adds a force to all objects that enter it's trigger in order to keep the object moving around the screen.
The problem is that these rigidbodies slow down and eventually stop responding to my script or to it's rigidbody component at some point. The object should continuously move around the playing field at random, but it eventually slows down and gets stuck in a corner. Sometimes, the gameObject noticeably slows down after a certain time which is why it could be a rigidbody component issue, however newly spawned gameObjects stop reacting to the boundaries as well so I'm not sure if it's a scripting issue.
Here's the script attached to the game boundary colliders:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ScriptWindArea : MonoBehaviour
{
public List<GameObject> AffectedObjects;
public Vector3 ForceVector;
void OnTriggerEnter(Collider collidee)
{
if(collidee.gameObject != null || collidee != null)
{
if(collidee.gameObject.tag != "Enemy")
{
AffectedObjects.Add(collidee.gameObject);
}
}
}
void OnTriggerExit(Collider collidee)
{
if(collidee.gameObject != null || collidee != null)
{
if(collidee.gameObject.tag != "Enemy")
{
AffectedObjects.Remove(collidee.gameObject);
}
}
}
void Update()
{
for(int I =0; I < AffectedObjects.Count; I++)
{
if(Time.timeScale > 0)
{
try
{
AffectedObjects[I].rigidbody.AddForce(ForceVector);
}
catch
{
return;
}
}
}
}
}
Could someone explain to me the right way to make sure a gameObject is continuously moving through the playing field, or explain to me why my script/gameObjects are not functioning properly?
I can't see in your OnTriggerEnter() where the force is being applied. What i would do, and maybe you could do is when an enemy has entered the collider, then apply the force there ins$$anonymous$$d of within the update.
Also if your rigidbodies are kinematic then it means that the object will not react to physics. $$anonymous$$aybe that could be a problem?
Correction: they are not kinematic. I just checked and I thought they were before.
Also, I just tested applying the force in OnTiggerEnter, but that makes matters worse.
Answer by Slatey · Feb 06, 2014 at 03:27 PM
do your rigid bodies also have a physics material attached or just the walls?
Both the walls and gameObjects have physic materials attached.
Answer by DvKrane · Feb 13, 2014 at 02:44 PM
I forgot to update this question as I have fixed my problem myself by adding a call to remove the missing gameobject in my catch statement like this: try { AffectedObjects[I].rigidbody.AddForce(ForceVector); } catch { AffectedObjects.Remove(AffectedObjects[I]); return; }
Your answer
Follow this Question
Related Questions
Same reaction with different number of objects colliding 2 Answers
Get a bounding sphere for collider/rigidbody? 0 Answers
Sprite does not rotate with a CircleCollider2D 1 Answer
HingeJoint with no rotation? 0 Answers
Creating a SIMPLE car. 1 Answer