- Home /
Moving an object based on the position of another object
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();
}
}
Answer by MarigoldFleur · Jun 03, 2012 at 06:05 AM
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.
Answer by MD_Reptile · Jun 03, 2012 at 05:13 AM
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);
}
Well I need the controller that slides up and down to move the object on its x axis, no rotation. So basically its like a slider, up and down movement makes a cube move left to right.
@r6834 oh okay I misunderstood, but... try the Space.Self part, and then try the smoothing the desired movement part, let me know how those work, and I updated my answer to better apply to your question
This solution sends my object flying off to the side. Essentially I just have the 3d up and down slider drag-able to a certain limit. I need this up and down movement to correspond with the cubes left and right translation on the x axis. So by default the cube starts in the center, as does the slider. Sliding up translates the cube left on the x-axis, sliding down translates it right on the x axis. Releasing the slider will bring the cube back to center, as well as the slider back to the center.
This is the code I have on a seperate gameobject that controls the two objects. The slider has its own script.
var aimObj : Transform;
var handleObj : Transform;
var moveXAmount : float;
unction Update () {
moveXAmount = handleObj.transform.position.y;
aimObj.Translate(moveXAmount, 0, 0);
if (firing == true) {
Fire();
}
}
Your answer
Follow this Question
Related Questions
Transform.Translate but ignore rotation on one axis 0 Answers
Lock Z Rotation. Character Controller. 1 Answer
Rotating Player 0 Answers
different axis for different objects? 1 Answer
Move and Rotate on a fixed axis? 1 Answer