I want to move an object on its x axis. The object needs to be controlled by a different object that can only move on the Y axis. So for instance I have a cube on the left, it is a rigid body you can only drag up and down on the Y axis. I want the Y axis movement on this object to move a different object on its X axis. The issue that I am having is that the X axis object starts in the wrong position. I want it to start in the position it is in, in the scene, and translate according to movement on the other objects Y axis. Any thoughts?
function Update () {
aimObj.position.x = controlObj.position.y * 4;
if (firing == true) {
Fire();
}
}
You’re directly translating the position of aimObject’s x position to controlObj’s y position * 4. Instead, first calculate the distance between the two objects and use that as an offset, rather than directly changing it like you are now.
What you need is to relatively move the other object, rather than setting the position to a specific number.
Like sort of like this:
var moveXAmount : float;
function Update()
{
moveXAmount = ObjectThatSlidesUpAndDown.transform.position.y;
ObjectThatMoves.Translate(moveXAmount, 0, 0, Space.Self); // Space.World is default if you dont put the fourth argument, which is absolute rotation
}
And that should move it based off its local position to begin with, atleast if my memory serves me properly haha…try this and get back if you dont get the desired result.
another thing is, this might continue to move it each frame…not sure exactly how you want the movement to take place, like do you want the moving object to move to a place the slider objects in, and stop there? If so you might take another approach…
To have the slider set the place it will move to, and then smooth the movement to land in a specific spot on the actual object, do something like this:
function Update()
{
desiredPos = ObjectThatSlides.transform.position.y;
currentPos = ObjectThatMoves.transform.position.x;
ObjectThatMoves.transform.position = Vector3.Lerp (currentPos, desiredPos, 0.5f);
}