c# - Unity 3D scripts move multiple objects -
i have problem. i'm doing project in unity 3d (c#), 3d worlds editor. problem want move multiple objects selecting them. managed move 1 mouse cursor, multiple failed :d code move 1 :
public class clickanddrag : monobehaviour { private raycasthit raycasthit; private gameobject gobj; private float distance; private vector3 objposition; private bool bobj; // use initialization void start() { } // update called once per frame void update() { if (input.getmousebutton (0)) { var ray = getcomponent<camera> ().screenpointtoray (input.mouseposition); var hit = physics.raycast (ray.origin, ray.direction, out raycasthit); if (hit && !bobj) { gobj = raycasthit.collider.gameobject; distance = raycasthit.distance; debug.log (gobj.name); } bobj = true; objposition = ray.origin + distance * ray.direction; gobj.transform.position = new vector3 (objposition.x, objposition.y, objposition.z); } else { bobj = false; gobj = null; } } }
thanks help!
private gameobject gobj;
variable single gameobject. reformat private list<gameobject> objects;
, instead of gobj.transform.position = new vector3 (objposition.x, objposition.y, objposition.z)
this:
foreach (gameobject item in objects) { item.transform.position = new vector3 (objposition.x, objposition.y, objposition.z) }
edit: in case aren't sure how manipulate list, list<t>
has set of built in functions make easy. can call objects.add(newobject);
add object, , objects.remove(oldobject);
remove object.
Comments
Post a Comment