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 zombience_unclaimed · May 04, 2012 at 07:35 PM · rigidbodyforcetuio

Objects continue to have force applied after escaping force range

The problem is this: I have a scene of a bunch of spheres bouncing around like particles in a contained area.

Via TUIO, I have a point that appears on the scene and repels particles, but only if they come within a close radius. This all works fine, except that when I put a TUIO point on the scene, the particles that get repelled by the TUIO point never stop being affected by the repulsion, no matter how far away they disperse, until the TUIO point disappears from the scene completely.

I have the repulsion script being called AFTER a check to see whether the particle is in range of the TUIO repulsor. If there is no TUIO repulsor in range, it returns null and so should not be repelling anything. Yet I find when looking in scene view, all the particles end up bouncing repeatedly against the walls, stuck there until the TUIO repulsor disappears.

I'd appreciate any help. Here's the code. Thanks!

 using UnityEngine;

using System.Collections;

public class FollowTUIO_RGBParticles : MonoBehaviour {

 GameObject[] repulsors;
 GameObject closestRepulsor;
 public float repulsionCutoff = 10f;
 ParticleSystem[] ps;

     float x;
 float z;
 Vector3 addedForce;

     void Update () {
 
     GameObject repulsor = getClosestRepulsor();
     applyForce(repulsor);
     
 }


void applyForce(GameObject repulsor) {
if(repulsor == null) { applyConstantRandomForce(); }
else {

         Vector3 difference = repulsor.transform.position - transform.position;
         float sqrDiff = difference.sqrMagnitude;
         float targetMass = repulsor.rigidbody.mass;
         
         rigidbody.AddForce( difference / -sqrDiff * targetMass);    
         rigidbody.velocity = vectorClamp(rigidbody.velocity, 1f);
         
         
     }    
 }


void applyConstantRandomForce() {

     //check to see if vector is beyond range of -1 to 1.  if so, reverse direction of random addition by making it a negative number
     if (addedForce.x > .01 || addedForce.x < -.01)
     {
         x *= -1;
     }
     if (addedForce.z > .01 || addedForce.z < -.01)
     {
         z *= -1;
     }
     
     addedForce = new Vector3(addedForce.x + x, 0f, addedForce.z + z);
     addedForce = vectorClamp(addedForce, 1.5f);
     constantForce.relativeForce = addedForce;
     //rigidbody.velocity = vectorClamp(rigidbody.velocity);
 }
 
 Vector3 vectorClamp(Vector3 curVelocity, float clampAmount)
 {    
     
     Vector3 clampedVelocity = Vector3.ClampMagnitude(curVelocity, clampAmount);
     return clampedVelocity;
 }
 
 GameObject getClosestRepulsor()
 {
 
     repulsors = GameObject.FindGameObjectsWithTag("Repulsor");
     
     
     float distance = repulsionCutoff;
     Vector3 position = gameObject.transform.position;
 
     foreach (GameObject repulsor in repulsors)
     {
         Vector3 diff = (repulsor.transform.position - position);
         float curDistance = diff.sqrMagnitude;
         
         //find nearest repulsion point
         if(curDistance < distance)
         {
             closestRepulsor = repulsor;
             distance = curDistance;
         }
     }
     return closestRepulsor;
 }
 

}

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Matt- · May 04, 2012 at 09:53 PM

closestRepulsor is never set back to null, after you set it once to any repulsor with < repulsionCutoff distance it will never go back to being null.

You could fix that a lot of ways, for example have getClosetsRepulsor return a temp value instead of setting the member field closestRepulsor.

 GameObject getClosestRepulsor()
 {
     GameObject found = null;
     repulsors = GameObject.FindGameObjectsWithTag("Repulsor");
 
     float distance = repulsionCutoff;
     Vector3 position = gameObject.transform.position;
 
     foreach (GameObject repulsor in repulsors)
     {
        Vector3 diff = (repulsor.transform.position - position);
        float curDistance = diff.sqrMagnitude;
 
        //find nearest repulsion point
        if(curDistance < distance)
        {
          found = repulsor;
          distance = curDistance;
        }
     }
     return found;
 }

note that here I'm returning the temp variable found now instead of setting the member. This way it can actually return null.

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 zombience · May 04, 2012 at 11:00 PM 0
Share

Ohhh I totally see it now! I've been looking at code for a liiiittle too long these last few days :p Thank you for pointing it out

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

6 People are following this question.

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

Related Questions

Reading the forces applied on a freezed rigidbody(solved) 1 Answer

Unity First person controller force and mass 0 Answers

maximum torque/maximum force 0 Answers

Add force on camera using AddForce 2 Answers

Rigidbody velocity has got different direction than VelocityChange force vector 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