You can download the project files by clicking on this link:
http://www.creativedesigndigital.co.uk/roboarm.zip
I have three objects:
- top cylinder
- main arm
- secondary arm
- scoop
They are in a hiierachy as follow:
topcylinder > mainarm > secondaryarm
When I push a key, i wish for all object to go through a series of simple roational movements and finish in a pre defined position, smoothly with some easing in and easing out of the rotation.
For exampe, the topcylinder will rotate 200 degrees, the mainarm will then rotate 45 degrees and at the same time, the secondaryarm will rotate to maintain a level orientation in space.
Here is my Script:
//Speed factors for each of the object - THESE DONT SEEM TO WORK…
var topCylinder_speed = 5.0;
var mainArm_speed = 5.0;
var secondaryArm_speed = 5.0;
//These variables monitor the angle of the objects for the ‘if’ statements in the 'Update() function…
var topCylinder_base_angle = 0.0;
var mainArm_base_angle = 0.0;
var secondaryArm_base_angle = 0.0;
//I intended to use these in the if statements but they dont work for some reason…
var base_angle_min = -45.0;
var base_angle_max = 200;
//Initializing the rotation amounts - NOT REALLY SURE IF THESE ARE RIGHT…
var topCylinderRotation = 0.0;
var mainArmRotation = 0.0;
var secondaryArmRotation = 0.0;
//Initialising the variable to hold the name of the seconaryarm. This is so we can find it for rotating it (see ‘Start ()’ and ‘defineObjectNamesGlobally()’ functions…
var secondaryArmName;
//On Start this function passes the name of the ‘secondaryarm’ object out to another function so that we can make it accessible through the Update() function…
function Start () {
var secondaryArmNameID = GameObject.Find(“secondaryarm”);
defineObjectNamesGlobally(secondaryArmNameID);
//Debug.Log("secondaryArmNameID: "+secondaryArmNameID);
}
//'secondaryArmName is set to the value obtained by the ‘GameObject.Find()’ function. We had to do it this way since Unity required the the ‘Game.Find()’ function be run from within the ‘Start()’ function…
function defineObjectNamesGlobally(secondaryArmNameID){
secondaryArmName = secondaryArmNameID;
//Debug.Log("secondaryArmName: "+secondaryArmName);
}
//Uses the data to rotate the objects when the key is pressed…
function Update () {
///Not sure if this is the best way to go about it, I dont fully understand…
topCylinderRotation = Time.deltaTime * topCylinder_speed * 20;
mainArmRotation = Time.deltaTime * mainArm_speed * 20;
secondaryArmRotation = Time.deltaTime * secondaryArm_speed * 20;
if (Input.GetKey(“s”)) {
//begin rotation of topcylinder… i find these angles all a but wild and unpredicatble… I would like to also make it slowly and smoothly ease in and out…
if (topCylinder_base_angle >= -200.0) {
transform.Rotate(0, topCylinderRotation, 0);
//Updating the rotational angle…
topCylinder_base_angle = topCylinder_base_angle - topCylinderRotation;
} else {
//Begin extending the mainarm once the cylinder reaches its full rotation… I would like to smooth these out…
if (mainArm_base_angle >= -45.0) {
transform.Find(“mainarm”).Rotate(0, mainArmRotation *-1, 0);
}
//This didnt work correctly but does now, the secondary arm used too shoot off to a bizzar angle!.. I want it to remain 90 degrees to the horizon…
if (secondaryArm_base_angle >= -45) {
//using the data obtained from the Start() function… (was not sure how to go to the second child in a hierachy)…
secondaryArmName.transform.Rotate(secondaryArmRotation, 0, 0);
}
mainArm_base_angle = mainArm_base_angle - mainArmRotation;
secondaryArm_base_angle = secondaryArm_base_angle - secondaryArmRotation;
}
}
Debug.Log(secondaryArm_base_angle);
}
Ideally, i’d rather be able to define a target point in space for the scoop to get to and the rest to just follow if that makes sense. For example in the scene their is a sphere, i’d like it to look for where that is and head for it. I also want it to be able to reverse back through the motions to go back ‘home’. Also, I’d like to add smoothing to all of the movements.
Thanks for all your help for this in advance!
Best,
Jonathan. ![]()