1
\$\begingroup\$

This simulates a GameObject swinging between two positions.

I am unable to manually move a GameObject on the Y axis using the Unity editor (in scene view) when the following script is attached and running, it's like a solid animation:

Vector3 min, max;

void Update()
{
    min = wall1.position + new Vector3(4,0,0); 
    max = wall2.position - new Vector3(4,0,0);

    transform.position = Vector3.Lerp (min, max, (Mathf.Sin(2 * Time.time) + 1.0f) / 2.0f);
}
\$\endgroup\$
3
  • 1
    \$\begingroup\$ You'll need to think about how you want to combine movement from multiple sources. The rule you're using now is "sine wave between the two walls always wins - all other movement is ignored." But you could have all kinds of other rules, like "sine wave wins on the x axis, but other sources can move the object on the y & z axes" or "other sources apply an offset to the sine wave, allowing it to get closer or further from its min & max" or many other options. What rule would you like the object to follow? \$\endgroup\$
    – DMGregory
    Commented Mar 12, 2016 at 1:01
  • \$\begingroup\$ @DMGregory Exactly, that's what I need: "sine wave wins on the x axis, but other sources can move the object on the y & z axes" How do we do it properly with code? \$\endgroup\$
    – Kosde
    Commented Mar 12, 2016 at 8:31
  • \$\begingroup\$ @DMGregory The current answer does not solve the problem. I need a proper revised version of my code. \$\endgroup\$
    – Kosde
    Commented Mar 12, 2016 at 9:15

2 Answers 2

2
\$\begingroup\$

When you assign a Vector3 to transform.position, you assign all three axises. To assign only X axis, you need to do this:

var oldPos = transform.position;
var newPos = new Vector3(__newXValue__, oldPos.y, oldPos.z);
transform.position = newPos;

Change __newXValue__ with what you want, and this code will only change x axis and other axises will not get effected by this code.

Note: Do not split these lines to different methods that will run on different frames, the integrity must remain intact. The reason for this is, if transform gets updated from any other source in between you get the old position and assign new position, your GameObject will teleport.

\$\endgroup\$
8
  • \$\begingroup\$ Thanks for the answer! But, I don't see how I can implement this logic with my own code. Can you give an answer with the code that I provided? \$\endgroup\$
    – Kosde
    Commented Mar 12, 2016 at 12:44
  • \$\begingroup\$ @Kosde Replace __newXValue__ with Vector3.Lerp method's X return. \$\endgroup\$ Commented Mar 12, 2016 at 13:47
  • \$\begingroup\$ It does not work, I'm getting errors. Here's the code that I made: var oldPos = transform.position; var newPos = new Vector3(Vector3.Lerp (min, max, (Mathf.Sin(2 * Time.time) + 1.0f) / 2.0f), oldPos.y, oldPos.z); transform.position = newPos; \$\endgroup\$
    – Kosde
    Commented Mar 12, 2016 at 13:53
  • \$\begingroup\$ var oldPos = transform.position; var newPos = new Vector3((Vector3.Lerp (min, max, (Mathf.Sin(2 * Time.time) + 1.0f) / 2.0f)).x, oldPos.y, oldPos.z); transform.position = newPos; \$\endgroup\$ Commented Mar 12, 2016 at 13:55
  • \$\begingroup\$ Thank you my friend, it works now. I love correct straightforward answers. \$\endgroup\$
    – Kosde
    Commented Mar 12, 2016 at 13:57
1
\$\begingroup\$

When your game is running, the Update() function update the GameObject's position every frame so you can't move it (actually you can move it but the update position it so fast that the move do not work).

You can try this: make this GameObject another GameObject's child in hierachy, and move the parent GameObject, it will work the way you want it.

\$\endgroup\$
3
  • \$\begingroup\$ This simply does not work. \$\endgroup\$
    – Kosde
    Commented Mar 12, 2016 at 9:09
  • \$\begingroup\$ As DMGregory well said: That's what I need: "sine wave wins on the x axis, but other sources can move the object on the y & z axes" How do we do it properly with code? Your answer does not solve the problem. I need a proper revised version of my code. \$\endgroup\$
    – Kosde
    Commented Mar 12, 2016 at 9:18
  • 1
    \$\begingroup\$ This should work, if you assign localPosition only on child. \$\endgroup\$ Commented Mar 12, 2016 at 12:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .