Script for butterfly roaming

Hello all

So I’m new to C# but would like to add a roaming butterfly in my game.

After some searching I found this script:

 var target:Vector3;
var timer:float;
var sec:int;
function Start () {
  target = ResetTarget();
  sec=ResetSec();
}
function Update () {
timer+=Time.deltaTime;
if(timer>sec){
target=ResetTarget();
sec=ResetSec();
}
transform.Translate(target*10*Time.deltaTime);
}
function ResetTarget():Vector3{
     return Vector3(Random.Range(-2.0,2.0),Random.Range(-2.0,2.0),Random.Range(-2.0,2.0));
}
function ResetSec():int{
     timer =0;
     return Random.Range(1,3);
}

however, since i’m still a beginner with C# I do not know how to put this in a full script.

Like with codeparts as show below added:

using UnityEngine;
using System.Collections;

public class Butterflyvlieg : MonoBehaviour

In short, how can I make this a full script and make my butterfly roam in my game.

Thanks in advance!

the script you posted is java, not C# that’s why you can’t get it to work.
I’ve converted it for you. I hope I didn’t miss anything. I didn’t test the code

using UnityEngine;
using System.Collections;
public class Butterflyvlieg : MonoBehaviour
{


//var target:Vector3;
private Vector3 target;
//var timer:float;
private float timer;
//var sec:int;
private int sec;

    void Start ()
    {
      target = ResetTarget();
      sec=ResetSec();
    }
   
    void Update ()
    {
        timer+=Time.deltaTime;
        if(timer>sec)
        {
            target=ResetTarget();
            sec=ResetSec();
        }
        transform.Translate(target*10*Time.deltaTime);
    }
   
    Vector3 ResetTarget()
    {
         return Vector3(Random.Range(-2.0,2.0),Random.Range(-2.0,2.0),Random.Range(-2.0,2.0));
    }
   
    int ResetSec()
    {
         timer =0;
         return Random.Range(1,3);
    }
}

Thanks a lot!

That explains haha.

However it seems something is wrong

I uploaded some pics:

should be:

return new Vector3(Random.Range(-2.0f,2.0f),Random.Range(-2.0f,2.0f),Random.Range(-2.0f,2.0f));

Thanks for your help!

This fixed the compiler error.

When I press play my butterfly however disappears, not sure if its position just automatically transforms to something totally different, but I can´t find it.


try this for the keeping the butterfly close to it’s current position.

return new Vector3(Random.Range(transform.position.x + -2.0f,transform.position.x + 2.0f),Random.Range(transform.position.y + -2.0f,transform.position.y + 2.0f),Random.Range(transform.position.z + -2.0f,transform.position.z + 2.0f));

Ya, no idea what happened. Is it possible that it’s under your ground/terrain or inside something?
If you click it in the hierarchy but still can’t find it, that is a mystery to me lol. I would look at its position there, and then compare that to the ground/terrain level… and if that doesn’t explain it, I’m not sure.

Is your butterfly a 3d model - just curious?

and the ‘new’ , I think :slight_smile: (with -2, and 2 we could have removed the zeroes, also and no ‘f’ heh).

Thanks both for your help,
really appreciated :slight_smile:

So I updated the script with the lines you gave me.

It seems something is still wrong. The position of the butterfly starts at a very high value position and then drops to lower numbers, also after a certain time it just freezes at a random position with lower numbers.

using UnityEngine;
using System.Collections;
public class BFscript : MonoBehaviour
{


    //var target:Vector3;
    private Vector3 target;
    //var timer:float;
    private float timer;
    //var sec:int;
    private int sec;

    void Start()
    {
        target = ResetTarget();
        sec = ResetSec();
    }

    void Update()
    {
        timer += Time.deltaTime;
        if (timer > sec)
        {
            target = ResetTarget();
            sec = ResetSec();
        }
        transform.Translate(target * 10 * Time.deltaTime);
    }

    Vector3 ResetTarget()
    {
        return new Vector3(Random.Range(transform.position.x + -2, transform.position.x + 2), Random.Range(transform.position.y + -2, transform.position.y + 2), Random.Range(transform.position.z + -2, transform.position.z + 2));
    }

    int ResetSec()
    {
        timer = 0;
        return Random.Range(1, 3);
    }
}

3324620--258939--bf4.JPG
3324620--258940--bf3.JPG

You modified the code to include "transform.position.x + / - 2 "
I tested your code and it went insane :slight_smile: lol
transform.Translate moves relative to self, so when you keep adding transform.position as part of the target, it grows out of control.
I’m testing it right now, and I don’t know exactly what you want, but you could keep the translate as target * 10 or change the number 10 to be a bit smaller. (and remove the transform.position parts).
It seems to be better than the other. Not sure if you want some kind of position clamping also, on any axis so it doesn’t go too far in any 1 particular way. :slight_smile: