- Home /
Script to Move different parts of Gantry robot in X,Y,Z axis in sequence
Hello Everyone, I am very new to programming. I am trying to do x,y, z movement of different parts of a Gantry robot in sequence in a single program but till now i have not succeeded. Can anyone help me in writing code to move different parts of a single object in X,Y,Z axis in sequence in a single c# program. Any help is highly appreciated.
Your question contains very little information about your actual robot. Just to clear the first uncertainty: Do you talk about a virtual 3d model of such a robot inside of your Unity project, or do you mean an actual real robot which you want to control from Unity? Hopefully you mean the first one.
As the others have mentioned in their answers having a single object with multiple parts contradicts each other. If you have multiple parts it can't be a single object.
It would help to have some more information about your robot:
A screenshot / image would help
A screenshot of the hierarchy (fully expanded) of the robot object would also be great.
Thanks a lot for your reply...To be more clear i have attached the photo of Gantry robot. Here, the part which is red has to move horizontally and the part which is black has to move horizontally and vertically (basically it's a pick and place robot). Can i script multiple motions in sequence in a single script? If yes, can you please guide me. (because i have succeeded in moving single motion at a time but not multiple motions operating one after the other)
Answer by Shippety · Jul 09, 2017 at 01:13 AM
Hey! I couldn't completely tell based on what you said, did you mean there are different objects that are part of the same object you're trying to move, or didja just mean you were trying to move on the different axis?
An easy way to move something on a single axis is just to use Transform.Translate() like this:
transform.Translate(Vector3.forward * Time.deltaTime);
If you want to move different objects different ways in the same script, just substitute whichever object.transform for 'transform' in the Translate code.
To control which axis / direction the object is translating, change 'Vector3.forward' to something else. Look under the static variables list here and you can see which term you'd like to use based on the axis you wanna change.
Hope that's helpful! If something doesn't make sense or I wasn't clear just tell me :)
Answer by Jwizard93 · Jul 09, 2017 at 01:57 AM
Shippety's advice is probably the best way to move the robot. I read your question as though you want each movement in one axis to complete before moving in the next. There are many ways to do this.
Detecting if something made it to a certain point can be frustrating because unity isn't so picky about precise positioning. For example if you say move to the right unitl transform.position.x == 5 it may never stop moving. why? Because it is very unlikely that it's position will be exactly 5 on any frame. You could use > and < instead but now you have to write it essentially twice to handle movement in the positive and negative directions.
You will probably end up doing this if you really must have it in one script.
Personally I'm dreaming up a system of colliders you instantiate at desired positions.
Thes colliders have a variable which is set upon their instantiation telling them which "waypoint" they are. They have script attached which uses OnTriggerEnter function to send the gantry arm on to the next waypoint.
This is just one of a thousand ways you can do this.