Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by DvKrane · Feb 06, 2014 at 02:30 PM · 2dphysicsrigidbodycollider

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?

Comment
Add comment · Show 3
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
avatar image APenguin · Feb 06, 2014 at 03:14 PM 0
Share

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?

Hope this helps you understand more about rigidbodies

avatar image DvKrane · Feb 07, 2014 at 12:52 AM 0
Share

Correction: they are not kinematic. I just checked and I thought they were before.

avatar image DvKrane · Feb 07, 2014 at 01:00 AM 0
Share

Also, I just tested applying the force in OnTiggerEnter, but that makes matters worse.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Slatey · Feb 06, 2014 at 03:27 PM

do your rigid bodies also have a physics material attached or just the walls?

Comment
Add comment · Show 1 · Share
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
avatar image DvKrane · Feb 07, 2014 at 12:49 AM 0
Share

Both the walls and gameObjects have physic materials attached.

avatar image
0

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; }

Comment
Add comment · Share
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

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

20 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

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


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