1
\$\begingroup\$

I have the following scenario:

a sphere is moving towards an object it can see (a cube). The cube is moving randomly through the landscape. The sphere will keep following it. I have a script that will check IF the cube is Within Sight. If it is, it will move towards it and it will keep following it.

What I want

I want a Debug.Log message to show when:

  1. When the sphere is moving towards a (new) target. it must say: "Searching..."
  2. When the sphere has found the cube, I want it to say: "Found the cube."
  3. When the cube is found, I want it to continue searching for new cubes. So it must say once more: "Searching..."

Problem:

It keeps showing these messages nonstop. I found a solution with a false bool in an IF statement. But, I don't know how to combine 2 messages.

Code:

using UnityEngine;
using BehaviorDesigner.Runtime;
using BehaviorDesigner.Runtime.Tasks;

public class MoveTowards : Action {

public float speed = 0;
public SharedTransform target;
private bool pietje;

private bool jaap;

void PrintMessage (string message) {
    Debug.Log(message);
}

void Start() {
    pietje = false;
}

public override TaskStatus OnUpdate() {

    if (Vector3.SqrMagnitude (transform.position - target.Value.position) < 0.1f && !pietje) {
        PrintMessage ("Found the cube.");
        pietje = true;
        return TaskStatus.Success;
    }


        transform.position = Vector3.MoveTowards (transform.position, target.Value.position, speed * Time.deltaTime);
        transform.LookAt (target.Value.position);
        return TaskStatus.Running;

}

}

I just don't know how to fix the last part where it says: "Searching..." I don't know how to implement it with an IF statement (doesn't work somehow) or how to create the IF statement (when I use this, it doesn't work:

if (TaskStatus.Running == 1 && !pietje)

Anyone that can help me? That would be awesome! Have been trying to fix this for some hours now....

\$\endgroup\$
2
  • \$\begingroup\$ It keeps showing these messages nonstop Which ones ? There is only one message displayed on this code. \$\endgroup\$
    – Aracthor
    Aug 30, 2016 at 12:20
  • \$\begingroup\$ @Aracthor Sorry, I tackled that problem with the false bool !pietje. Currently I only can enable the message "found the cube". I don't know how to implement the message "searching...". I don't know how and where to use this code. Any tips? \$\endgroup\$ Aug 30, 2016 at 12:51

1 Answer 1

1
\$\begingroup\$

Supposing your OnUpdate function always either find or search, you can print your searching message when you don't print your Found the cube message:

if (Vector3.SqrMagnitude (transform.position - target.Value.position) < 0.1f && !pietje) {
    PrintMessage ("Found the cube.");
    pietje = true;
    return TaskStatus.Success;
}

    if (pietje)
    {
        pietje = false;
        PrintMessage ("Searching...");
    }

    transform.position = Vector3.MoveTowards (transform.position, target.Value.position, speed * Time.deltaTime);
    transform.LookAt (target.Value.position);
    return TaskStatus.Running;
\$\endgroup\$
2
  • \$\begingroup\$ Thank you for your comment! The problem here is that "Searching..." is looped infinitely long in the console log. I want it to show the message only once \$\endgroup\$ Aug 30, 2016 at 14:01
  • \$\begingroup\$ @JuliusSecret Changed code to make it print only once. Should alternate between your two messages now. \$\endgroup\$
    – Aracthor
    Aug 30, 2016 at 14:08

You must log in to answer this question.

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