Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Script for butterfly roaming

Discussion in 'Scripting' started by LuukArts, Dec 14, 2017.

  1. LuukArts

    LuukArts

    Joined:
    Nov 20, 2017
    Posts:
    7
    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:

    Code (CSharp):
    1.  var target:Vector3;
    2. var timer:float;
    3. var sec:int;
    4. function Start () {
    5.   target = ResetTarget();
    6.   sec=ResetSec();
    7. }
    8. function Update () {
    9. timer+=Time.deltaTime;
    10. if(timer>sec){
    11. target=ResetTarget();
    12. sec=ResetSec();
    13. }
    14. transform.Translate(target*10*Time.deltaTime);
    15. }
    16. function ResetTarget():Vector3{
    17.      return Vector3(Random.Range(-2.0,2.0),Random.Range(-2.0,2.0),Random.Range(-2.0,2.0));
    18. }
    19. function ResetSec():int{
    20.      timer =0;
    21.      return Random.Range(1,3);
    22. }
    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:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. 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!
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    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

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class Butterflyvlieg : MonoBehaviour
    4. {
    5.  
    6.  
    7. //var target:Vector3;
    8. private Vector3 target;
    9. //var timer:float;
    10. private float timer;
    11. //var sec:int;
    12. private int sec;
    13.  
    14.     void Start ()
    15.     {
    16.       target = ResetTarget();
    17.       sec=ResetSec();
    18.     }
    19.    
    20.     void Update ()
    21.     {
    22.         timer+=Time.deltaTime;
    23.         if(timer>sec)
    24.         {
    25.             target=ResetTarget();
    26.             sec=ResetSec();
    27.         }
    28.         transform.Translate(target*10*Time.deltaTime);
    29.     }
    30.    
    31.     Vector3 ResetTarget()
    32.     {
    33.          return Vector3(Random.Range(-2.0,2.0),Random.Range(-2.0,2.0),Random.Range(-2.0,2.0));
    34.     }
    35.    
    36.     int ResetSec()
    37.     {
    38.          timer =0;
    39.          return Random.Range(1,3);
    40.     }
    41. }
     
  3. LuukArts

    LuukArts

    Joined:
    Nov 20, 2017
    Posts:
    7
    Thanks a lot!

    That explains haha.

    However it seems something is wrong

    I uploaded some pics: error2.JPG error.JPG
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    should be:
    Code (csharp):
    1.  
    2. return new Vector3(Random.Range(-2.0f,2.0f),Random.Range(-2.0f,2.0f),Random.Range(-2.0f,2.0f));
    3.  
     
  5. LuukArts

    LuukArts

    Joined:
    Nov 20, 2017
    Posts:
    7
    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.
     

    Attached Files:

    • bf2.JPG
      bf2.JPG
      File size:
      86.1 KB
      Views:
      671
    • bf1.JPG
      bf1.JPG
      File size:
      58.1 KB
      Views:
      708
  6. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    try this for the keeping the butterfly close to it's current position.

    Code (CSharp):
    1. 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));
     
    Last edited: Dec 14, 2017
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    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?
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    and the 'new' , I think :) (with -2, and 2 we could have removed the zeroes, also and no 'f' heh).
     
  9. LuukArts

    LuukArts

    Joined:
    Nov 20, 2017
    Posts:
    7
    Thanks both for your help,
    really appreciated :)

    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.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class BFscript : MonoBehaviour
    4. {
    5.  
    6.  
    7.     //var target:Vector3;
    8.     private Vector3 target;
    9.     //var timer:float;
    10.     private float timer;
    11.     //var sec:int;
    12.     private int sec;
    13.  
    14.     void Start()
    15.     {
    16.         target = ResetTarget();
    17.         sec = ResetSec();
    18.     }
    19.  
    20.     void Update()
    21.     {
    22.         timer += Time.deltaTime;
    23.         if (timer > sec)
    24.         {
    25.             target = ResetTarget();
    26.             sec = ResetSec();
    27.         }
    28.         transform.Translate(target * 10 * Time.deltaTime);
    29.     }
    30.  
    31.     Vector3 ResetTarget()
    32.     {
    33.         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));
    34.     }
    35.  
    36.     int ResetSec()
    37.     {
    38.         timer = 0;
    39.         return Random.Range(1, 3);
    40.     }
    41. }
     

    Attached Files:

    • bf4.JPG
      bf4.JPG
      File size:
      13.1 KB
      Views:
      731
    • bf3.JPG
      bf3.JPG
      File size:
      13.8 KB
      Views:
      702
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You modified the code to include "transform.position.x + / - 2 "
    I tested your code and it went insane :) 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. :)