0
$\begingroup$
public class ChangeDirection : MonoBehaviour {
public bool movePositive = true;

// Update is called once per frame
void Update () {
    // CHECK IF MAX REACHED AND MOVING POSITIVE DIRECTION
    if (this.transform.position.y <= 4f && movePositive) {
        // MOVES IN POSITIVE DIRECTION
        this.transform.position += Vector3.up * Time.deltaTime * 5f;
    } 
    else {
        movePositive = false;
    }
    // CHECK IF MIN REACHED AND MOVING NEGATIVE DIRECTION

    if (this.transform.position.y >= -4f && !movePositive) {

        // MOVES IN NEGATIVE DIRECTION
        this.transform.position += Vector3.down * Time.deltaTime * 5f;
    } 
    else {
        movePositive = true;
    }
 }
}

NOT C-SHARP QUESTION EXACTLY - I NEED TO IMPLEMENT A BOOL LOGIC IN PYTHON The code above moves an object back and forth within specified limit making one bool switching True and False, Hits the max and moves in negative direction and hits the min and moves in positive direction.

But as you can see in the image, I can't do the same thing with the same logic. How can it be implemented in Python script with Animaton Node - Blender?

Blender AN - Move Back and Forth Script

Adding the code extra if you have problem with image

move_pos = True

if input_x < 20 and move_pos:
    input_x += 1
else:
    move_pos = False

if input_x > -20 and not move_pos:
    input_x -= 1
else:
    move_pos = True

output_x = input_x
$\endgroup$

2 Answers 2

0
$\begingroup$

I see no good reason to discretize such simple animation. A parametric approach might be better. To move an object between 0 and 1 in 20 frames, you can use such node tree:

Node Tree

To see why it works, plot the function. Another approach would be to use the animation nodes in Animation Nodes:

Node Tree 2

$\endgroup$
5
  • $\begingroup$ I can't see the images. They are not opening. It is for learning purposes to compare stuff between two different platforms. Trying to understand the approach in general with scripting. $\endgroup$
    – vGuMuS
    Commented Mar 4, 2018 at 16:32
  • 1
    $\begingroup$ @vGuMuS They are working fine for me, they are gifs. If your question is how to convert C# into python, then I think it is better asked in stackoverflow. $\endgroup$
    – Omar Emara
    Commented Mar 4, 2018 at 16:41
  • $\begingroup$ Okay I can see them now. I like your approach but I would like to see the process which can be done using script in AN. It is not a C sharp question. I need a logic with bool switching. $\endgroup$
    – vGuMuS
    Commented Mar 4, 2018 at 18:03
  • $\begingroup$ By the way I checked the first node tree. Do we really need division node? It works just fine without it. $\endgroup$
    – vGuMuS
    Commented Mar 4, 2018 at 18:32
  • $\begingroup$ @vGuMuS Divide is just to make the interval into 0 1, but if you want it as is, then you can remove it. $\endgroup$
    – Omar Emara
    Commented Mar 4, 2018 at 18:34
0
$\begingroup$

It took me a while but I could come up with a solution creating a class. Including class method makes it static-like and increases the performance compared to calling the function on class object.

The interesting part; I had to use "time info" as an input in a "for loop" otherwise it just moves to the end point.

enter image description here

enter image description here

$\endgroup$

You must log in to answer this question.

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