- Home /
Apply DragRigidBody.js to only a certain group of objects and only in a specific area
Hi, new here and very very limited javascript skills. I'm trying to work everything out myself or via tutorials/forums but I'm stuck since a few days here: Is there an easy way to tell the bundled DragRigidBody.js to apply to only a certain group of objects (tagged)? My other question would be if one can limit the area to which the script applies ( I basically want to shoot an obect but not let the user control the thrown object in mid-air). Thanks a lot in advance!
Answer by robertbu · Feb 21, 2014 at 12:52 AM
Create a new JS script file with a name like 'MyDragRigidbody'. Copy and paste the contents of the DragRigidbody.js script into this new file. You will be using this file in place of the original DragRigidbody.js script.
Insert at line 25:
if (hit.collider.tag != "WhateverTagYouWant")
return;
And below that, you can do your check:
if (hit.transform.position.y > someValue)
return;
I'm not sure how you will define the acceptable area to start dragging. I've done it here by the 'y' value, but you may want something else. Regardless, figure out your check, and just return if the criteria is not met.
Thank you so much, that already helped! I was so confused by the if and then negative thing but thanks, got my head around that ;)
The area thing works as well, the only thing is I need the dragging to stop as soon as the obect is dragged beyond the area that "dragging is allowed" (so when the mouse is kept down during the drag). Do you think you could help me with that as well or is that more complicated? Thanks!
I don't know what you test for 'area' is. But say for example the test was the distance from some point you define at the top of the file. Find this line (line 61 in the original script):
while (Input.Get$$anonymous$$ouseButton (0))
Change it to:
while (Input.Get$$anonymous$$ouseButton(0) && Vector3.Distance(centerPointYouDefined, springJoint.transform.position) < someDistance)
$$anonymous$$y guess is you are going to want some other condition, but the idea is to ter$$anonymous$$ate this while loop when the object is outside the area.